-
Notifications
You must be signed in to change notification settings - Fork 313
Behavior differs between local and non-local properties with no default value #1064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm actually not sure why it behaves this way. I can't think of a good reason for the language to do this; might okay to make this behave like a normal property (default to the type's default value) |
I think that you are just putting the value without just explaining the old value which is not providing the first value any point |
@HT154 What’s the use case? Unlike a regular property, a local property cannot be amended in place. I can’t think of many valid reasons to set a local property to the default object value, so it might pay off to be explicit. |
I think amending is orthogonal here; normal properties will default to the type's default value. a: A Is effectively the same as: a: A = new A {} It's strange that this works: a: A
b = a But this suddenly breaks: local a: A
b = a Also, By the way, welcome back! |
True, except that many types don’t have default values and always need to be assigned explicitly.
Under the assumption that default values are rarely useful for local/fixed/const properties, requiring explicit assignment will avoid mistakes. I don’t have a strong opinion on this issue, but being selectively more restrictive can sometimes be a tolerable/beneficial form of inconsistency. |
My use case here is, once again, abusing the language to get more namespacing without more modules. Sorry! I have a bunch of similar looking but unrelated types and I want to build sets of mixins for them. To that end, I have a module that look like this: // mixins.pkl
class MixinsForA {
const mix1: Mixin<A> = new { ... }
const function mix2(arg): Mixin<A> = new { ... }
}
class MixinsForB {
const mix1: Mixin<B> = new { ... }
const function mix2(arg): Mixin<B> = new { ... }
} Usage looks like this: import "mixins.pkl"
local a: mixins.MixinsForA = new {}
local b: mixins.MixinsForB = new {}
// someA |> a.mix1 |> a.mix2("foo") A couple key goals here are having a single import point and reducing the amount of code needed to use/reference each mixin. |
Our Kubernetes package has fixed properties without explicitly assigned values: https://github.com/apple/pkl-k8s/blob/65907044bcf76ff0347abbb84e307fd291e0ad70/generated-package/api/core/v1/Pod.pkl#L29-L31 I'm not sure what errors we might be preventing here; you still get an error if the property's type has no default, but only when accessed. I think that better follows Pkl's lazy ethos. Also, parse-time checks like this can't be caught using We can (and probably should) add an IDE error if a local property's type has no default value, which is the same as what we do with
Yeah, makes sense, good food for thought. |
When I see For module properties, defaulting to
I can’t tell if you were implying otherwise, but I believe that IntelliJ should only give an error if parsing/evaluation is guaranteed to produce an error. Otherwise, IntelliJ should give a warning. |
We only show an error annotation if we know it will error during eval. And, a local property whose type has no default, will indeed raise an error during eval (as long as the property is executed). |
Repro:
I would expect
aLocal
to be initialized to the default value ofA
here instead.This error is thrown during parsing, so it's not even necessary to have
aLocal
in the evaluation path to trigger it.Also pkl-intellij does not register this as an error, only
pkl eval
.The text was updated successfully, but these errors were encountered: