pkl icon indicating copy to clipboard operation
pkl copied to clipboard

Type parameters in `new Mapping<TypeA, TypeB>`/ `new Listing<Type>` are not checked

Open bioball opened this issue 3 months ago • 3 comments

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" }

bioball avatar Apr 05 '24 23:04 bioball

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.

translatenix avatar Apr 12 '24 20:04 translatenix

That's less simple than you might think. The generic informs the default / IDE type checking. It's a common pattern to define Listings inline to produce something of a very different type, such as new Listing<String> { ... }.join(", ") to produce a String.

holzensp avatar Apr 16 '24 14:04 holzensp

Dealing with generics is never simple. Regarding your concrete arguments, Listing.join is defined for all Listings, 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.

translatenix avatar Apr 17 '24 05:04 translatenix