question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for default parameter values in generic

See original GitHub issue

This 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:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
travisbrowncommented, Nov 4, 2016

This is now available in circe-extras in 0.6.0-RC1.

1reaction
travisbrowncommented, Sep 18, 2015

@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 like decoderWithoutDefaults in semiauto.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found