associated-types Questions

1

I have an existential type defined like this: trait Collection { type Element; } impl<T> Collection for Vec<T> { type Element = T; } type Existential<T> = impl Collection<E...
Calque asked 24/4, 2019 at 9:25

4

Solved

In the following code, I want to test if x is a SpecialController. If it is, I want to get the currentValue as a SpecialValue. How do you do this? If not with a cast, then some other technique. Th...
Durante asked 2/11, 2016 at 19:12

1

I want to write code for an enum of stationary that takes default values when nothing is mentioned. Is it possible to do so in Swift? Here's an example of what I'm trying to do public enum Stationa...
Mahaliamahan asked 18/6, 2022 at 11:39

2

In Swift, the following code compiles without issue. protocol P1 { associatedtype T = Int } protocol P2 { typealias T = Int } To me, these appear to behave almost identically. The only differ...
February asked 10/4, 2019 at 22:26

2

Solved

I can define an associated function foo for an array like this: pub trait T { fn foo(); } impl<X> T for [X; 2] { fn foo() { panic!("Whatever") } } But how do I now call this fun...
Ichthyosis asked 10/4, 2022 at 16:47

2

As far as I know, the definition of the ViewModifier protocol looks like this: protocol ViewModifier { // content view type passed to body() typealias Content // type of view returned by body(...
Korte asked 10/9, 2020 at 12:19

4

Solved

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 a...

1

Solved

I'm working on an app which needs to query multiple APIs. I've come up with classes for each API provider (and in more extreme cases, a class for each specific API Endpoint). This is because each A...
Copenhaver asked 20/6, 2018 at 9:59

3

Solved

I'm in the process trying to learn and understand protocols with associated types in swift. At the same time, I'm learning SwiftUI and taking a course on Udemy. The app that we will building is a...
Chela asked 26/10, 2019 at 11:42

1

I have a function that returns an impl Trait so I don't have access to the concrete return type. I need to use the return value of that function as an associated type in a trait. How do I do that? ...
Bajaj asked 16/4, 2019 at 15:37

1

I have a Swift protocol MessageHandler with an associated type, a few different Message types, and also a class X: protocol MessageHandler { associatedtype Message func handle(message: Message...
Pulchritude asked 8/4, 2019 at 15:35

1

Solved

I'm trying to make my API Service as generic as possible: API Service Class class ApiService { func send<T>(request: RestRequest) -> T { return request.parse() } } So that the compi...
Mllly asked 14/3, 2019 at 16:26

2

Solved

I want to write a function that accepts Iterator of type that has ToString trait. What I have in mind: fn parse<T: Iterator /* ?T::Item : ToString? */>(mut args: T) -> Result<String, ...
Calgary asked 18/1, 2019 at 16:48

1

Solved

I'm trying to define a trait with an associated type. I also want the associated type to implement Iterator with its Item associated type implementing AsRef<str>. While I know how to do it f...
Rialto asked 1/1, 2019 at 19:6

2

Solved

I'm writing a generic wrapper class for core data. Here are some of my basic types. Nothing special. typealias CoreDataSuccessLoad = (_: NSManagedObject) -> Void typealias CoreDataFailureLoad ...
Alvord asked 12/12, 2018 at 16:3

1

Solved

When I want to check if a type conforms to a simple protocol, I can use: if let type = ValueType.self as? Codable.Type {} When the protocol has associated type, for example RawRepresentable has ...
Abaddon asked 12/10, 2018 at 9:42

2

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 tex...
Cherianne asked 9/10, 2018 at 21:52

1

Solved

This snippet is valid in Rust 1.26.1: use std::ops::AddAssign; trait Trait where for<'a> Self: AddAssign<Self> + AddAssign<&'a Self> + Sized, { } trait Trait2 { type Asso...
Undrape asked 2/6, 2018 at 20:29

2

Solved

I'm trying to achieve the effect of Kotlin sealed class in Swift, so that I can implement a class-based alternative to enums with associated types. The following results in a compiler error: fina...
Achernar asked 2/1, 2017 at 16:7

2

Solved

When I'm extending Collection the type of count is IndexDistance. When I'm extending Array type the count is of type Int Why is there such a distinction? Is this a recent change or it's always be...
Fragrance asked 28/3, 2018 at 16:47

1

Solved

I'd like to know how this type of relation (example in kotlin) would be expressed in Swift interface Index<K, V> { fun getAll(key: K): Sequence<V> } I tried to use protocols with as...
Oersted asked 23/2, 2018 at 23:8

1

I'm very new to Rust so I may have terminology confused. I want to use the hashes crates to do some hashing and I want to dynamically pick which algorithm (sha256, sha512, etc.) to use at runtime....
Scirrhous asked 29/12, 2017 at 20:0

1

Solved

RFC 1623, stabilized in Rust 1.17.0, made it so that we do not have to explicitly specify the 'static lifetime in a static or const: const MY_DEFAULT_NAME: &str = "Anna"; // ^ Look, ma! No 'st...
Blanc asked 1/10, 2017 at 19:55

1

Solved

I need to create generic function in protocol with default implementation in extension. It func should work with item as enum:RawRepresentable where RawValue == String always. I tried protocol Req...

2

Solved

I have this source: pub fn draw<G, C>(&self, font: &mut C, draw_state: &DrawState, transform: Matrix2d, g: &mut G) where C: CharacterCache, G: Graphics<Texture = <C a...
Ship asked 5/7, 2017 at 14:21

© 2022 - 2024 — McMap. All rights reserved.