mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Refer to functions through their identifiers #183
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 @hmonfleur on GitHub (Jul 10, 2024).
I want to refer to functions through their identifiers.
Declaring a function and a class:
Instantiating the following object:
produces:
Cannot find property `foo`.Being unsure of how to refer to "foo" I also tried:
Which produces:
Cannot find property `foo` in module `text`.However the following code:
produces:
Am I missing something or is it the expected behavior ?
@holzensp commented on GitHub (Jul 12, 2024):
Unfortunately, this is more complicated than you'd hope. Functions and properties have entirely different namespaces in Pkl. This means that you can have a function
foo()and a propertyfooside-by-side without issue, but it also means that, notationally, functions are not as first-class as you'd like (believe me, I've wanted this too). If you want to pass afunctionas a function-typed-value, you have to wrap it in a lambda;Here's a thing to consider (and I'm mindful you probably reduced the real example to get here); whenever you write a zero-argument function in Pkl, alarm bells should be ringing. You never need those; since there are no side-effects, there's no reason to postpone evaluation "manually," and since Pkl is late-bound, all properties essentially are zero-arity functions.
@bioball commented on GitHub (Jul 22, 2024):
Closing this; if you need to refer to a function as a value, use a lambda notation like @holzensp mentioned. Methods (those that start with a
functionkeyword) can't be passed as a value.