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

macro based Modifier for Record #567

Closed
wants to merge 13 commits into from
21 changes: 20 additions & 1 deletion core/src/main/scala/shapeless/hlists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package shapeless

import scala.language.dynamics
import scala.language.experimental.macros

import scala.annotation.tailrec
import scala.reflect.macros.whitebox

Expand Down Expand Up @@ -105,12 +104,14 @@ object HList extends Dynamic {
def selectDynamic(tpeSelector: String): Any = macro LabelledMacros.hlistTypeImpl

@tailrec
@deprecated("used for binary compatibility with 2.3.0", "2.3.1")
def unsafeGet(l: HList, i: Int): Any = {
val c = l.asInstanceOf[::[Any, HList]]
if(i == 0) c.head
else unsafeGet(c.tail, i-1)
}

@deprecated("used for binary compatibility with 2.3.0", "2.3.1")
def unsafeUpdate(l: HList, i: Int, e: Any): HList = {
@tailrec
def loop(l: HList, i: Int, prefix: List[Any]): (List[Any], HList) =
Expand All @@ -123,6 +124,24 @@ object HList extends Dynamic {
val (prefix, suffix) = loop(l, i, Nil)
prefix.foldLeft(suffix) { (tl, hd) => hd :: tl }
}

//TODO: remove @noinline after switching to ScalaJs 0.69+
@noinline
def unsafeCrud(l: HList, i: Int, f: Any => Any, remove:Boolean): (Any, HList) = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really prefer this to be called unsafeUpdateWith.

@tailrec
def loop(l: HList, i: Int, prefix: List[Any]): (List[Any], HList, Any) =
l match {
//add case
case HNil => if(remove)throw new Exception("Index out of bounds.Cannot remove.") else (prefix, f(null) :: HNil, null)
//remove / modify case
case hd :: (tl: HList) if i == 0 => if(remove)(prefix, tl, hd) else (prefix, f(hd) :: tl, hd)
//recursion step
case hd :: (tl: HList) => loop(tl, i - 1, hd :: prefix)
}

val (prefix, suffix, v) = loop(l, i, Nil)
v -> prefix.foldLeft(suffix) { (tl, hd) => hd :: tl }
}
}


Expand Down
Loading