[英]How to make type-erased version of a trait with associated type?
假設有一個集合特征,其項具有關聯類型:trait CollectionItem { // ... } trait Collection { type Item: CollectionItem; fn get(&self, index: usize) -& ...
[英]How to make type-erased version of a trait with associated type?
假設有一個集合特征,其項具有關聯類型:trait CollectionItem { // ... } trait Collection { type Item: CollectionItem; fn get(&self, index: usize) -& ...
[英]Can I have a collection over trait objects where their associated types implement a shared trait?
我知道通常你不能有像Vec<dyn Iterator>>這樣的東西,因為你當然需要指定關聯的類型。 現在,我當然可以擁有Vec<Box<dyn Iterator<Item=i32>>> 。 但是這樣的事情呢:trait Foo {} #[de ...
[英]How to add constraint on associated type in Swift
我正在嘗試創建一個協議R ,其中包含一個對象數組n ,原則上,這些對象可以是不同的類( N1 , N2 ),但所有主題都屬於同一協議N 。 提交給協議R ( M1 , M2 ) 的類可以決定它們的屬性數組n是否只包含特定種類的R或任何。 這編譯得很好: 但是我不明白如何向它必須符合N的關聯Ntype ...
[英]Member 'changeAccountConfirmDialog' expects argument of type 'Author'
如果 changeAccountConfirmDialog 中有參數,我應該如何實現這個變量的功能。 我得到的錯誤是“成員'changeAccountConfirmDialog'需要'Author'類型的參數” ...
[英]Use Protocol with associatedtype as a Type
我有這樣的協議 我有這樣的具體結構。 TitleTypeView和ButtonTypeView具有相同的超級 class。 我必須將 Builders 結構組合到 HeaderBuilder 我收到了這個錯誤無法將“HeaderTitleBuilder”類型的值轉換為預期的參數類型“某些 View ...
[英]return View in swift protocol function with associatedtype
我正在嘗試在這里創建一個可重用的協議。 我收到錯誤,這些錯誤在此處添加為評論。 在網上找不到任何解決方案來解決這個問題。 我怎樣才能使這段代碼工作? 或者有什么建議可以在哪里查找有關它的更多信息? 謝謝! ...
[英]Defining associated type in sub protocol vs generic type constraint
直接定義從另一個協議繼承的協議中的關聯類型與使用泛型類型約束有什么區別? 例如 BI 在 TestB 中得到這個錯誤: 成員“read”不能用於“any RepositoryB”類型的值; 考慮改用通用約束有人可以解釋其中的區別嗎? 根據我的理解,這應該是相同的,因為它也期望符合它的實體實現相同的實 ...
[英]swift protocol method when associated meets a constraint
我有一個相關類型的協議。 我希望協議僅在關聯類型符合 Equatable 時才強制執行某些方法。 像這樣的東西: 我只是找不到正確的語法。 我也嘗試了擴展,但編譯器(正確地)聲稱 function 主體。 ...
[英]compiler "has no member" error with MVVM design pattern Swift
我正在嘗試為 MVVM 架構編寫樣板代碼,並且我正在嘗試在我的 ViewModel 中創建 View 接口,而不是在它們之間綁定屬性。 我確實收到一個編譯器錯誤, Type 'V.Command' has no member 'reload'如何改進我的代碼,以便在我的 ViewModel 中抽象 ...
[英]Two protocols with associated types: How to link their implementations in a type-safe way?
目標在為我的應用程序創建網絡層時,我通常使用通用方法從服務器獲取數據,例如 我發現這非常優雅,因為我只需要實現一次我的 fetch 方法,但仍然使用此協議獲得類型安全: 這樣,我可以輕松地在任何地方添加新資源(端點),而無需修改現有代碼,並且包含 API 端點的資源始終通過關聯類型直接與結果的Mo ...
[英]Why does associatedtype not conform to protocols defined in its constraining protocol?
我正在嘗試讓我的 SwiftUI 視圖更“可預覽”,因此我正在使它們在其商店 (ViewModel) 上通用,這樣我就可以更輕松地模擬它們。 考慮以下示例:public protocol HomeViewStore: ObservableObject { associatedtype An ...
[英]Defining concrete associatedtype but I still can't use protocol as variable
我想使用SendMessageUseCase作為變量類型,例如var sendMessageUseCase: SendMessageUseCase但我不能。 我不斷收到錯誤Protocol 'SendMessageUseCase' can only be used as a generic con ...
[英]Unconstrained type parameter implementing IntoIterator
我如何使用泛型類型參數實現 IntoIterator 而不會出現這種錯誤,我認為與此處的錯誤相同,但提出的解決方案在此上下文中無效,同時在Counter上執行一個名為iter的方法可以解決問題但它不會是慣用的#[derive(Debug, Clone, Copy, PartialEq, Eq, Pa ...
[英]Associated type for ndarray arguments in rust
我想為一個(數字)算法創建一個接口,我想為其提供一個帶有ndarray和類似庫的實現(比如pytorch 綁定) struct A<D> { array: D } trait<D> T { type ArgType; fn foo(&mut se ...
[英]How to call associated function with array or tuple type?
我可以為這樣的數組定義關聯的 function foo : 但是我現在怎么稱呼這個 function 呢? 我注意到像[usize;2]::foo()這樣的語法是無效的。 ...
[英]How do I obtain a trait object from a wrapper trait object with an associated type?
我有兩個特征Foo和Bar : 實際上,我會有許多不同類型的Foo和Bar ,但每個Foo都有一種Bar類型的相關特征。 現在保持簡單, SimpleFoo與SimpleBar相關聯: 在某些地方我可以使用 generics 和單態,但在某些地方我需要動態調度,比如這個 function 比需要一 ...
[英]Is it possible to assign multiple values in an enumeration? Swift language
我做了這樣的事情: 我需要這樣做: 我試圖這樣做,但沒有奏效:c ...
[英]returning swift protocol associated type in multiple methods
來自 java/kotlin 背景,我試圖了解具有相關類型的 swift 協議。 為什么以下代碼段無法編譯並出現錯誤:“類型 ViewFactoryImpl 不符合協議 ViewFactory”,解決方案是什么? 通過玩耍,我能夠通過定義以下內容來編譯 我不明白這個問題。 我認為關聯類型是為整個協議 ...
[英]How to find the associated type `IntoIer` when implementing `IntoIterator` by using iterator composition in Rust?
我目前正在嘗試實現 arrays 的結構。 我想以一種即時生成基本結構的方式為 SOA 實現 IntoIterator,就好像我在迭代結構數組一樣。 這是 to 類型:結構及其 SOA, 通過使用“izip!” 來自itertools的宏,通過壓縮我的 SOA 的Vec並將生成的元組映射到原始結構 ...
[英]Define a protocol type variable in another protocol
我想在其他協議p3和 class c1中定義類型為協議p1的變量v1 ,它符合協議p3我想從另一個協議p2定義v1 ,它是p1的子協議。 以正常方式我得到了這個錯誤類型“c1”不符合協議“p3” 你有什么解決辦法嗎? ...