docs

#E0146: 把具名 some 型別當作其具體型別使用

在宣告 type X = some Tr; 的模組之外,X 是一個獨立的型別,只具有 Tr:它的具體型別無法指名,其他型別的值也不是 X。把值保持為 X(欄位、let x: X、List<X> 和參數都能存放它),透過 Tr 使用它,並從 X 所在模組的函式取得新的值。需要具體型別的程式碼應放在該模組中。

nabi
mod shapes {
    pub type Count = some Display;

    pub fn count() -> Count { 3 }
}

let n: i64 = shapes::count();
error[E0146]: mismatched types: `let n` expects `i64`, found `Count`: outside `shapes`, `Count` is opaque
  --> main.nabi:7:14
   |
 7 | let n: i64 = shapes::count();
   |              ^^^^^^^^^^^^^^^
   |
   = help: only `shapes` sees what `Count` is: keep the value as `Count` and use it through its bounds, get one from `shapes::count`, or move this code into `shapes`

保持為 Count:

nabi
mod shapes {
    pub type Count = some Display;

    pub fn count() -> Count { 3 }
}

let n: shapes::Count = shapes::count();
println(f"{n} shapes");
3 shapes

相關錯誤碼:E0040 型別不符、E0145 不是由其模組定義的具名 some 型別

發現錯誤? 在 GitHub 上編輯此頁 最後更新: