Are Generic types not actual types? #858
-
I am trying to define a converter so that I can render some files in a custom format. I'm running into some issues when using generic type parameters to select different types for my converter. Here's a very simplified example of what I'm talking about: words = Listing<String> {
"1"
"2"
"3"
}
numbers = Listing<Int> {
4
5
6
}
myRenderer = new JsonRenderer {
converters {
[Listing<String>] = (i) -> toInt(i)
}
}
output {
files {
["words.json"] {
value = words
renderer = myRenderer
}
["numbers.json"] {
value = numbers
renderer = JsonRenderer
}
}
} This fails to evaluate with the following error:
Is this expected? Is there something that separates generic types that prevents them from being used in like this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The issue you're running into here is that types aren't values. When defining a converter, you're actually asked to provide a class value, and not a type. Here's the API docs for converters: https://pkl-lang.org/package-docs/pkl/current/base/ValueRenderer.html#converters So, in the below snippet: converters {
[String] = (it) -> /* etc */
} We are defining a mapping entry whose key, [Listing<String>] = (i) -> toInt(i) This is invalid syntax, because |
Beta Was this translation helpful? Give feedback.
The issue you're running into here is that types aren't values.
When defining a converter, you're actually asked to provide a class value, and not a type. Here's the API docs for converters: https://pkl-lang.org/package-docs/pkl/current/base/ValueRenderer.html#converters
So, in the below snippet:
We are defining a mapping entry whose key,
String
is referencing an object of typeClass
. It's not actually a type. That's also why Pkl is erroring when you define this key:This is invalid syntax, because
Listing<String>
cannot be interpreted as a value. And, going a little further, Pkl's converters don't provi…