sbt / sbt-boilerplate

sbt plugin for generating scala.Tuple/Function related boilerplate code
BSD 2-Clause "Simplified" License
109 stars 19 forks source link

Support 1-arity not being a Tuple1 #23

Closed dwijnand closed 7 years ago

dwijnand commented 7 years ago

I have some boilerplate to generate that revolves around case class apply/unapply. But the signature for unapply for a 1-arity product is Option[T], compare:

scala> case class CC1(x: Int); CC1.unapply _
defined class CC1
res0: CC1 => Option[Int] = $$Lambda$1308/693434027@58fbd02e

scala> case class CC2(x: Int, y: Int); CC2.unapply _
defined class CC2
res1: CC2 => Option[(Int, Int)] = $$Lambda$1313/233142783@7971c2a9

scala> case class CC3(x: Int, y: Int, z: Int); CC3.unapply _
defined class CC3
res2: CC3 => Option[(Int, Int, Int)] = $$Lambda$1314/1361241279@23e61112

Therefore I can't find a way to write a template that calls .1/.2/.. for 2+ arity, but skips it for 1-arity.

For the real world case, see https://github.com/eed3si9n/sjson-new/pull/62.

ktoso commented 7 years ago

We handle this in Akka by simply having the "1 case" (or the "0 case") implemented in full, and the others which are "exactly the same but * N" are handled using the repeat syntax.

You can put that 1 case in the same template file ofc too.

jrudolph commented 7 years ago

Yep, this plugin is mostly useful if the constructions are the same for all arities. If you need wrapping for arities >= 2 and no wrapping for arity == 1 then you need to write it out manually.

Otherwise, you can use Tuple1(a) instead of the (a,b,c) syntax to create tuples even for the Tuple1 case.

To skip generating for the 1-case you can use the [2..# syntax as e.g. used here:

https://github.com/akka/akka-http/blob/master/akka-http/src/main/boilerplate/akka/http/scaladsl/server/util/TupleFoldInstances.scala.template#L26

(Closing, as I think we cannot do anything here, but feel free to reopen if that's not correct.)

dwijnand commented 7 years ago

I was hoping to explore some possible syntax or maybe some special Tuple1 handling.

But I'm not familiar enough with the project to have any complete suggestions to offer.

jrudolph commented 7 years ago

Might be out of scope as this project has no idea about semantics (for Scala or anything else) at all. It's just a stupid text-based generator. ;)