associated-types

Variable of array which holds objects conform to protocol with associated type

非 Y 不嫁゛ 提交于 2021-02-10 07:37:06
问题 I am trying to build my own Form builder. It was going well until the point when I wanted to get a Value from a Cell . I wanted to make it generic as possible, for instance some Cell s could be responsible of ValueType of Int and others could be String . Anyways, my all Cell s basically conforms to protocol called BaseCell . I assigned a associatedType to this protocol and added a variable value which would return different types for each Cell . After some tryings I come with with the

Specifying associated type in trait that inherits from another trait

不问归期 提交于 2021-01-27 15:01:37
问题 I started working on my first more ambitious Rust project, and struggle with something I did not come across in any of the resources and tutorials I used for learning. The title of the question captures the abstract problem, but for the examples I'll use the concrete examples I am fighting with. For my project, I need to interface with different third-party services, and I decided to use the actix framework as an abstraction for the different actors in my domain. The framework defines the

How to combine F-bounded polymorphism with associated types in Scala?

牧云@^-^@ 提交于 2020-06-01 05:49:11
问题 I have a trait called Graphlike for things that work as a graph. Notably, one of the properties I want to have is that the method g.subgraph(Set(1, 2, 3)) would return a subgraph of the same type with just the vertices 1, 2 and 3. Apparently, this means that I want F-bounded polymorphism and that Graphlike looks something like this: trait Graphlike[A <: Graphlike[A]] { type Vertex def subgraph(selectedVertices: Set[Vertex]): A } I also have a trait representing an automaton with associated

Swift - Associated value or extension for an Enum

守給你的承諾、 提交于 2020-03-17 05:25:02
问题 General question regarding swift enum. I want to create an enum of "icon" and "associate" a value to the enum case enum Icon { case plane case arrow case logo case flag } I want to create an associated image to the enum's value. And also an associated color to the enum value So for instance if it was possible to do something like: extension Icon.plane { var image = { get { return UIImage("plane.png") } } var color = { get { return UIColor.greenColor() } } } var image = Icon.arrow.image // the

How can I correctly use associatedType in my protocol

徘徊边缘 提交于 2020-01-16 02:50:50
问题 I’m trying to come up with an protocol-oriented MVVM for my tableviewcells. I have lots of them. my viewModel protocol PlainTableViewCellModelType { var backgroundColor : UIColor {get} var textColor: UIColor {get} var titleFont : UIFont {get } var accessoryType : UITableViewCellAccessoryType {get} var textLabelNumberOfLines: Int {get} } my view protocol PlainTableViewCellType{ associatedtype viewModel : PlainTableViewCellModelType func setupUI(forViewModel viewModel: viewModel) } my class

How can I correctly use associatedType in my protocol

最后都变了- 提交于 2020-01-16 02:50:27
问题 I’m trying to come up with an protocol-oriented MVVM for my tableviewcells. I have lots of them. my viewModel protocol PlainTableViewCellModelType { var backgroundColor : UIColor {get} var textColor: UIColor {get} var titleFont : UIFont {get } var accessoryType : UITableViewCellAccessoryType {get} var textLabelNumberOfLines: Int {get} } my view protocol PlainTableViewCellType{ associatedtype viewModel : PlainTableViewCellModelType func setupUI(forViewModel viewModel: viewModel) } my class

How do I disambiguate associated types?

邮差的信 提交于 2020-01-14 09:17:30
问题 My current code looks like this: pub trait A {} pub trait HasA { type A: A; fn gimme_a() -> Self::A; } pub trait RichA: A {} pub trait RichHasA: HasA { type A: RichA; fn gimme_a() -> Self::A; // ... more things go here ... } pub fn main() {} My goal is to be able to use RichHasA as a HasA . The above code fails to compile with: error[E0221]: ambiguous associated type `A` in bounds of `Self` --> src/main.rs:10:21 | 3 | type A: A; | ---------- ambiguous `A` from `HasA` ... 9 | type A: RichA; |

Protocol with associatedtype Protocol for Generic functions

ⅰ亾dé卋堺 提交于 2020-01-03 02:28:26
问题 Is it possible to provide confirming protocol in generic functions of another protocol? I tried make it work like this, but it's impossible, or I made some mistakes. My code: protocol DataModelProtocol { associatedtype ObjectProtocol: Protocol func fetchObjects<T: ObjectProtocol>() -> [T]? func fetch<T: ObjectProtocol>(object: T) -> T? func delete<T: ObjectProtocol>(allObjectOf type: T.Type) func insert<T: ObjectProtocol>(_ object: T) func save<T: ObjectProtocol>(_ object: T) func update<T: