Rewriting with value classes

5 views
Skip to first unread message

etorreborre

unread,
Mar 17, 2017, 7:32:41 AM3/17/17
to kiama
Hi Tony,

I am trying to do rewrites on an AST where some nodes are value classes and this fails in the `dup` method.

My current workaround is

override def dup[T <: Product](t : T, children : Array[AnyRef]) : T =
super.dup(t, children map unbox)

/** value classes must be unboxed before duplication */
def unbox(s: AnyRef): AnyRef = {
val klass = s.getClass
s match {
case p: Product if Modifier.isFinal(klass.getModifiers) && p.productArity == 1 =>
p.productElement(0).asInstanceOf[AnyRef]
case _ =>
s
}
}

I don't think this is great because it would fail on the other hand it will fail on rewriting a final case class with one argument.
Unfortunately we don't know if a "Class" is a AnyVal at this stage.

What do you think?

Eric.

Tony Sloane

unread,
Mar 27, 2017, 6:41:14 AM3/27/17
to ki...@googlegroups.com
Hi Eric,

Thanks for pointing this out. I guess we don’t use AnyVals much here -)

I think a proper solution will require some thought. Your idea of unboxing gets there in some cases but I agree that’s it’s imprecise. 

A better variant might be to only try the unboxing approach if running the constructor on the new children fails *and* the number of arguments was correct. But that will still fail if one of the children is a final case class with a single argument.

Anyway, I’ve created an issue so we can discuss off list:


cheers,
Tony
--
You received this message because you are subscribed to the Google Groups "kiama" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kiama+un...@googlegroups.com.
To post to this group, send email to ki...@googlegroups.com.
Visit this group at https://groups.google.com/group/kiama.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages