我試圖在Swift中實現Kotlin密封類的效果,以便我可以實現基於類的替代方法來使用關聯類型的枚舉。 編譯錯誤的結果如下: final class Foo {
class Bar: Foo {} // Error: inheritance from a final class "Foo"
}
有沒有辦法來有效地「密封」的進一步子類一類的雨燕,但仍允許子類擺在首位?
我試圖定義與返回具有相同的壽命作爲一個參數相關聯的類型的函數的性狀的功能的性狀。 概念上類似如下(不工作:lifetime parameter not allowed on this type [Self::Output]): trait Trait {
type Output;
fn get_output<'a>(&self, input: &'a i32) -> Self:
是否可以要求某個類的關聯類型同義詞是某個其他類的實例?例如。用類似下面的代碼(不編譯): class Test a where
type Foo a
instance Show (Foo a)
我將能夠依靠的事實Foo a是Show能夠無論有關特定a的。 很明顯,我可以在類中添加更多方法來確保我想要的操作在Foo a上,但能夠重用現有類將會很好。
我有一個雨燕3項目中,我聲明瞭一個協議與關聯的類型是這樣的: protocol ViewModelContainer {
associatedtype ViewModelType
var viewModel: ViewModelType! { get set }
}
而且我要檢查,如果兩個物體執行ViewModelContainer及其關聯的類型ViewModelType
比方說,我們有一個協議定義爲: protocol PAT {
associatedtype Element
}
,我也有一個枚舉(典型的結果)定義爲: enum Result<Value> {
case success(Value)
case error(Error)
}
現在我想添加當Element是Result<Value>但是編譯器不能確定Value