Generics in Val #734
Replies: 9 comments 18 replies
-
Sorry for the late reply.
IIUC what template template parameters are, I think the answer is yes, using type requirements (a.k.a. associated types) in traits. If you point me to a concrete example, then I might be able to give a more comprehensive and definitive answer.
Yes.
That's on our wishlist but we haven't defined them yet.
Yes, using conditional extensions and conformances.
Yes, but note that operations like copying, moving, or destructing will require a trait bound. |
Beta Was this translation helpful? Give feedback.
-
template template parameters correspond to higher-kinded types, whose capabilities are not covered by Swift associated type requirements. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer! So, if there's nothing corresponding to HKTs, will there be other features to give similar power to generics? |
Beta Was this translation helpful? Give feedback.
-
Val's support for generics will be very similar to Swift's, so you might have a look here for some details. Currently, the two most significant improvements we have in the pipes are scoped conformances and generic value parameters. Scoped conformance lets us declare conformance of a type to a trait (in Swift, a protocol) in a specific scope. Generic value parameters let us express things like "a buffer of 12 integers" ( |
Beta Was this translation helpful? Give feedback.
-
Regarding HKT, I'm still on the fence as to whether or not the feature is worth its complexity. I think that associated types + opaque types + overloaded return types already offer a decent solution for most common use cases, like homomorphisms. For example, assume we define a trait Collection {
/// The type of the elements in the collection.
type Element
/// Returns a collection containing the elements in `self` transformed by `transform`.
fun map<T>(_ transform: (Element) -> T) -> some Collection where .Element == Element
} Then we could have an implementation of conformance Array: Collection {
// This satisfies the requirement since `Array<T> <: some Collection where .Element == T`
fun map<T: Sinkable>(_ transform: (Element) -> T) -> Array<T> { ... }
}
conformance Set: Collection {
// This satisfies the requirement since `Set<T> <: some Collection where .Element == T`
fun map<T: Sinkable>(_ transform: (Element) -> T) -> Set<T> { ... }
}
public fun main() {
let s: Set([1, 2])
let t = s.map(String.new(describing:))
// The dynamic type of `t` is `Set<String>`
} Of course, at compile time, the use site doesn't know that
|
Beta Was this translation helpful? Give feedback.
-
I didn't feel like writing an augmented
or I don't know that the ability to create any |
Beta Was this translation helpful? Give feedback.
-
I'd like to highlight several Swift's generics flaws so they could be considered beforehand.
Protocols in Swift support associated types, but there is no way to express a requirement for this type to be generic itself. The lack of this prevents composition of generics. A real world example why is it bad could be found in the Swift's stdlib. There is the
Pretty much like the generic associated types this expresses the requirement of Also, I have some thoughts about
Or
|
Beta Was this translation helpful? Give feedback.
-
Sure that's obvious, but for its weight, this feature should enable some nontrivial algorithms to be written that couldn't be expressed well otherwise. I don't think deriving [Also |
Beta Was this translation helpful? Give feedback.
-
btw, will function/subscript types be a part of the common type system or they will be kinda separate? This is a broad question to discuss, but in general there are only three questions:
|
Beta Was this translation helpful? Give feedback.
-
I have some questions about generics, mainly about how powerful would they be in val.
Beta Was this translation helpful? Give feedback.
All reactions