That's a great choice ;-) — if I had the time, this would be the Swift-feature
I'd spend it for…
I hope you have the stamina to finish that, and although it's no real help for
implementation, I have some remarks that might be useful in the long run.
There are already some thoughts on the topic online (afair even core had
something to say about it — I can try to find it if you didn't do so already
;-).
Your Vector example illustrates one problem: "T: X" can now have two different
meanings; either T is a subtype of X, or T is a literal of type X.
Even if the compiler can figure out what meaning to chose, it would help humans
to have a slightly different syntax.
There's also the idea of labeled generic parameters, and default values for
them.
Both aren't required, but imho at least the labels might be something that
people request before another kind of generics is accepted.
> struct Vector<T: ExpressibleByIntegerLiteral, L: IntegerLiteralExpr> {
> var elements: [T]
> init() {
> elements = [T](repeating: 0, count: L)
> }
> }
>
> let vect = Vector<Int, 5>()
I have strong hope that literal generics will form the basis for fixed-size
vectors, so that there will be no need to define such a type ;-) — but besides
that:
Why is T restricted here? Even if it should be a numeric type, I would allow
floats as well.
> And, once that's working, I'm going to add support simple "type functions":
> func join <T, L1, L2> (_ lhs: Vector<T, L1>, _ rhs: Vector<T, L2>) ->
> Vector<T, L1 + L2 > {...}
> I think restricting the supported "type functions" to expressions that could
> be evaluated by the compiler's "constant folding" code would be a reasonable
> place to start, until we figure out what we want to do about
> "pure"/"constexpr" stuff... even just "+" for numeric and string literals,
> and "-" for numeric literals, seems like a reasonable starting goal, and I
> think that'd be simple enough to implement (famous last words, right?)...
> It's all academic until I get the simple cases working first, though.
Even without such calculations, the feature would be quite useful:
For type-safe matrices, it would already be enough to have the dimensions
attached to the type.
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev