Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for default parameter values in generic #65

Closed
trane opened this issue Sep 17, 2015 · 8 comments
Closed

Support for default parameter values in generic #65

trane opened this issue Sep 17, 2015 · 8 comments
Milestone

Comments

@trane
Copy link

trane commented Sep 17, 2015

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)
)
@alexarchambault
Copy link
Contributor

Something like milessabin/shapeless#459 would allow to address that (default field values can easily be pulled like in this test).

@travisbrown
Copy link
Member

One general question: should this be the behavior provided by generic.auto (if default values are available in the Default instance, use them), or should it be an option provided by semiauto?—i.e. something like this:

implicit val decodeBar: Decoder[Bar] = deriveFor[Bar].decoderWithDefaults

@trane
Copy link
Author

trane commented Sep 18, 2015

I don't know what the benefit is to having it in semiauto. The least surprising option, from my point of view, is to treat case class X with/without defaults the same way.

@travisbrown
Copy link
Member

@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.

@trane
Copy link
Author

trane commented Sep 18, 2015

@travisbrown don't trust my point of view, I've been coding in Ruby for the past 2 years -- of course I like magic and surprising behavior!

@travisbrown travisbrown added this to the 0.3.0 milestone Nov 5, 2015
@travisbrown travisbrown modified the milestones: 0.4.0, 0.3.0 Feb 13, 2016
@LATaylor-guardian
Copy link

LATaylor-guardian commented Oct 18, 2016

@travisbrown @trane Was this ever added by any chance? I was playing around this morning and hit this issue so was unsure if I am missing something?

@ilya-murzinov
Copy link

@travisbrown I was figuring out how it can be implemented and ended up with the following.
Inside DerivationMacros#decodeHList and DerivationMacros#decodeCoproduct we should check whether the class has default values, and if it has, add them to representation.
Am I on the right path?
If so, can you give any tips in how it can be done?

@travisbrown
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants