Support for default parameter values in generic
See original GitHub issueThis appears to be mostly shapeless related, but @travisbrown asked me to file this for posterity…
case class Foo(s: String, i: Int = 10)
val json = """{"s": "Hello"}"""
// this obviously works
decode[Int => Foo](json).map(_(10))
// but what about cases where you have many default parameters of the same type?
case class Bar(i1: Int, i2: Int = 2, i3: Int = 3, i4: Int = 4)
val json2 = """{"i1": 1}"""
// i would like the following behavior...
"""{"i1": 1, "i4": 44}""".as[Bar] // => Bar(1, 2, 3, 44)
// this would work kind of work, but it overrides the default values
decode[(Int, Int, Int) => Bar].map(_(2, 3, 44))
// alternatively, I could do this to ignore the potentially incoming values:
val dec = Decoder.instance[Bar](c =>
for {
i1 <- c.downField("i1").as[Int]
} yield Bar(i1)
)
// or change my datatype completely:
case class Bar2(i1: Int, i2: Option[Int], i3: Option[Int], i4: Option[Int])
val dec2 = Decoder.instance[Bar2](c =>
for {
i1 <- c.downField("i1").as[Int]
i2 <- c.downField("i2").as[Option[Int]]
i3 <- c.downField("i3").as[Option[Int]]
i4 <- c.downField("i4").as[Option[Int]]
} yield Bar2(i1, i2, i3, i4)
)
Issue Analytics
- State:
- Created 8 years ago
- Reactions:5
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Default parameters with generics in TypeScript - Stack Overflow
If I wanted to make Lion the default value for createInstance() how would I do this? For example: class Animal { numLegs: number...
Read more >Generic Parameter Defaults in TypeScript - Marius Schulz
TypeScript 2.3 implemented generic parameter defaults which allow you to specify default types for type parameters in a generic type.
Read more >[Draft] Allow default value for parameters in generic clause
Introduction. Allow type parameters in generics clauses to have default values in class definition. Could be expanded to generic functions also.
Read more >Support default values for generic function and classes type ...
Support default values for generic function and classes type parameters ; Type, Feature F ; Target versions, No Target versions ; State, To...
Read more >Functions | Kotlin
Overriding methods always use the base method's default parameter values. When overriding a method that has default parameter values, the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is now available in circe-extras in 0.6.0-RC1.
@trane It’s not necessarily obvious that a derived decoder will use default constructor values (when they’re needed), and if that’s not the behavior you want, it could be an unpleasant surprise. But I agree—I think it makes sense to use them (with plenty of documentation) in
auto
, and to provide something likedecoderWithoutDefaults
insemiauto
.