No access to default arguments in Mirrors #17893
Replies: 16 comments
-
I agree, but I believe this is also feature requests. We should probably look at it only once we have revamped default parameters in Tasty. |
Beta Was this translation helpful? Give feedback.
-
(Has that happened, by any chance?) |
Beta Was this translation helpful? Give feedback.
-
Not yet, it's still the same encoding with name mangled methods |
Beta Was this translation helpful? Give feedback.
-
Any progress here? This is definitely a pain point when attempting to serialize case classes with default arguments to/from json (which is a fairly common scenario in practice). |
Beta Was this translation helpful? Give feedback.
-
no one is working on this as far as I know. Do you have a specific API in mind? I could imagine something untyped at least like |
Beta Was this translation helpful? Give feedback.
-
yes, Play JSON, but I assume any Scala 3 json library that attempts to extract case class defaut arguments at compile time is affected. |
Beta Was this translation helpful? Give feedback.
-
Changing the default parameter scheme is a very large undertaking. I would guess on the order of 3-6pm. I won't have the time to do it personally. I am not sure how we will be able to fund/resource this. Suggestions are welcome. |
Beta Was this translation helpful? Give feedback.
-
While this is not implemented see this solution using macros: https://stackoverflow.com/questions/68421043/type-class-derivation-accessing-default-values |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if this makes anything easier, but a slightly more limited option would be to expose a fromNamedProduct method on mirrors that takes a map from names to values. You wouldn't be able to access the default values directly, but you could at least construct an instance where the default are filled in for you. Might that avoid the need for any major refactors? |
Beta Was this translation helpful? Give feedback.
-
The problem is that the "default values" are hidden in default getter methods. And, if the default depends on previous parameters it will be a function over those parameter values instead of being a simple value. For the moment, I think the only solution would be the one @Lasering describes. |
Beta Was this translation helpful? Give feedback.
-
Thanks, that much I understood. I don't follow why a compiler-generated |
Beta Was this translation helpful? Give feedback.
-
Yes, I see. |
Beta Was this translation helpful? Give feedback.
-
I think @adampauls idea was to (my emphasis):
For |
Beta Was this translation helpful? Give feedback.
-
The companion object of every case class compiled by scala 3 is also the Mirror, |
Beta Was this translation helpful? Give feedback.
-
Ah, rats. I thought perhaps Mirrors were only generated for case classes with a derives clause. |
Beta Was this translation helpful? Give feedback.
-
It looks like there is also no flag for a default getter in |
Beta Was this translation helpful? Give feedback.
-
Minimized code
Output
Expectation
I would expect a way to access to default arguments, such that it may be used in a derived typeclass instance.
Beta Was this translation helpful? Give feedback.
All reactions