mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Support for type annotations on non-local object properties of a Class #115
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @etaham on GitHub (Mar 18, 2024).
[Feature Request]
I'm trying to organize the contents of a class and running into an error around type annotations. Here is the code:
Running
pkl eval bugReport.pkl -f yamlyields the following error:Something that does work is to define a new class for internalObj, but then you can't take advantage of late binding to set defaults without slightly less natural code (where the definition and defaults needs to be defined separately). This works, but notice that the default is specified outside the class definition.
Ask would be to make the first code snippet work.
Thank you!
Eli
@holzensp commented on GitHub (Mar 18, 2024):
One problem here is that your first code snippet defines an implicit class for
internalObject. What would you expectreflect.Class(new BugReport {}.internalObj.getClass()).nameto come out as?@etaham commented on GitHub (Mar 18, 2024):
good point - how does reflection work if not using type annotations?
Could the same be done for internal classes? If not, could it just generate "random" classname for the internal class?
@bioball commented on GitHub (Mar 19, 2024):
We don't have any plans on supporting type annotations on non-local object properties.
One feature that might help you is anonymous classes; e.g.
It's still unclear if we'd ever support this, but there's some valid use-cases for it. For example, for easier interop with external schema definitions (e.g. JSON Schema)
@odenix commented on GitHub (Mar 19, 2024):
@etaham
internalObj: InternalObj = new InternalObj{can be simplified tointernalObj: InternalObj = new {.@etaham commented on GitHub (Mar 19, 2024):
I'm trying to balance two goals:
Does this make sense? How are others thinking about this?
@odenix commented on GitHub (Mar 19, 2024):
I think you've already found the right solution.
If
BugReportis an important class that has many "groups", i.e., smaller classes, you can turn it into its own module:BugReport.pkl
@etaham commented on GitHub (Mar 19, 2024):
That works - it's just a little inconsistent.
regularVargets its type annotation and default in one spot, whilewillWorkwill be split.Thank you!