pkl
pkl copied to clipboard
Type parameters in `new Mapping<TypeA, TypeB>`/ `new Listing<Type>` are not checked
This does not throw, but should:
foo = new Listing<Int> {
"hello"
}
In the below, both types should be checked:
foo: Listing<Int(isEven)> = new Listing<Int(this > 5)> { 1; 2; 3 }
The check should not follow when amended.
This is fine:
foo: new Listing<Int> { 1; 2; 3 }
bar = (foo) { "hello" }
This should probably be fine too:
foo = new Listing<Int> { 1 } { "hello" }
A pragmatic solution would be to allow x: Listing<A> = new Listing {...}
but not x = new Listing<A> {...}
. This would reflect the (current) reality that generics aren't reified. It would also encourage users to do the right thing and put type annotations on their properties. It might even allow to phase out new Type {...}
in favor of (Type) {...}
, which in my view is a nicer literal syntax.
That's less simple than you might think. The generic informs the default / IDE type checking. It's a common pattern to define Listing
s inline to produce something of a very different type, such as new Listing<String> { ... }.join(", ")
to produce a String
.
Dealing with generics is never simple. Regarding your concrete arguments, Listing.join
is defined for all Listing
s, not just Listing<String>
. And IDEs could still try to infer the element type from the given elements. The default sounds like a bigger problem, but maybe there is an acceptable solution.