Closed jsundin closed 2 weeks ago
The IntelliJ plugin uses a static analyzer and can only detect some constraint violations. To have the element type checked by the evaluator, you need to write local randomValues: Listing<RandomValue> = new {
(see open issue).
This is an instance of https://github.com/apple/pkl/issues/405
new Foo {}
only derives what default value to amend from Foo
; it does not check the amended result. This is one of the reasons we recommend to not foo = new Foo {}
, but rather foo: Foo = new {}
.
Closed as duplicate of #405
TypeConstrDef.pkl:
TypeConstrImpl.pkl:
Expected results: the "localValues" variable contains two elements which do not validate against the RandomValue type, and should not be allowed.
Actual results: IntelliJ correctly picks up that the literal string ("i-love-pkl") is not valid, but fails to invalidate the second string reference. The "pkl eval" command (as well as the JDK library) allows for both values.
Without having any intimate knowledge about Pkl, I can only assume that this has to do with lazy evaluations and that the values in "randomValues" are actually only validated once they are rendered in "listOfStrings" (which is valid, "because string"). That seems a bit counter-intuitive, and would mean that the IntelliJ plugin is incorrect. Also, if we create a more basic TypeConstrImpl2.pkl we will get a different behaviour:
This results in a validation error from both IntelliJ and "pkl eval":
Notice that the error is on line 6, which could support the theory that this is related to lazy evaluation (otherwise I would expect the error to be on line 3, where the local declaration is). But if that's the case, I would not expect an error at all, as this should be equally wrong (or correct) as the Listing example.
(The example has been boiled down to bare essentials from our real configuration, the actual model is a bit more complex. But basically what we're trying to do is enforce strict formatting constraints on some parts of the configuration as an aid during configuration. The application using the list contains logic capable of dealing with these differences.)
The above has been tested using the following versions:
Pkl 0.25.3 (Linux 5.15.0-1053-aws, native)
Pkl 0.26.0-dev+3a31188 (Linux 5.4.0-177-generic, native)