Replies: 2 comments 2 replies
|
Using a subclassing approach would mean this will no longer typecheck (because you are passing a local myDeps: Listing<DependencySpec> = new {
// etc
}
dependencies {
["bar"] = myDeps
}It also makes it harder to interop between Pkl and external data (e.g. parsing JSON into Pkl structs). Rather, it sounds like you're looking for It extension methods. The pipe operator ( function satisfiedBy(meta: Metadata, packageName: String): (Listing<DependencySpec>) -> Boolean = (it: Listing<DependencySpec>) ->
let (package = meta.packages[packageName].package)
it.any((spec) -> spec.name == package.name && spec.version == package.version)Then, used like so: package.dependencies["bar"] |> satisfiedBy(meta, "bar") |
|
Another way to support "specialized listings/mappings" would be to generalize the object model to allow user-defined classes with elements/entries, a feature I proposed in the past. This would also turn |
Uh oh!
There was an error while loading. Please reload this page.
Hey all,
I would like to propose that the standard library classes
ListingandMappingbecome open, so that they can be extended. This would not allow adding properties to the listings/mappings (as they can only accept elements and entries respectively), but rather allow adding methods to the chain.Why?
This enables the use-case of "specialized listings/mappings", where the listings/mappings can be given additional functionality and can be asked questions. Ideal example from an internal project we have:
Workarounds
The alternative to the example above would be having a
Listingproperty inAlternatives:While this doesn't require any changes to the language, it:
output,What do you think? Alternatively, it would be cool if one could attach methods to type aliases which would also solve this, but that's another discussion.
All reactions