-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6399e82
commit 855899c
Showing
4 changed files
with
155 additions
and
10 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
shared/src/main/scala/io/estatico/newtype/arrays/AsArray.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.estatico.newtype.arrays | ||
|
||
import scala.reflect.ClassTag | ||
|
||
/* | ||
/** Type class for building Arrays for newtype N. */ | ||
trait AsArray[N] { | ||
/** Repr type of newtype N */ | ||
type Repr | ||
def classTag: ClassTag[Repr] | ||
final def empty: Array[N] = Array.empty(classTag).asInstanceOf[Array[N]] | ||
final def apply(xs: N*): Array[N] = | ||
Array(xs.asInstanceOf[Seq[Repr]]: _*)(classTag).asInstanceOf[Array[N]] | ||
final def cast(array: Array[Repr]): Array[N] = array.asInstanceOf[Array[N]] | ||
final def uncast(array: Array[N]): Array[Repr] = array.asInstanceOf[Array[Repr]] | ||
} | ||
object AsArray { | ||
type Aux[R, N] = AsArray[N] { type Repr = R } | ||
def empty[N](implicit arr: AsArray[N]): Array[N] = arr.empty | ||
def apply[N](xs: N*)(implicit arr: AsArray[N]): Array[N] = arr(xs: _*) | ||
def cast[R, N](a: Array[R])(implicit arr: AsArray.Aux[R, N]): Array[N] = arr.cast(a) | ||
def unsafeDerive[R, N](implicit ct: ClassTag[R]): AsArray.Aux[R, N] = new AsArray[N] { | ||
override type Repr = R | ||
override def classTag: ClassTag[R] = ct | ||
} | ||
} | ||
*/ |
67 changes: 67 additions & 0 deletions
67
shared/src/main/scala/io/estatico/newtype/arrays/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.estatico.newtype | ||
|
||
import scala.reflect.ClassTag | ||
|
||
package object arrays { | ||
|
||
// type AsArray[N] = AsArray.Type[N] | ||
// object AsArray { | ||
// | ||
// type Type[N] <: Base with Tag | ||
// type Base = Any { type Repr } | ||
// trait Tag | ||
// type Aux[N, R] = Type[N] { type Repr = R } | ||
// | ||
// private def clsTag[N](implicit ev: AsArray[N]): ClassTag[ev.Repr] = | ||
// ev.asInstanceOf[ClassTag[ev.Repr]] | ||
// | ||
// | ||
// def unsafeDerive[N, R](implicit ct: ClassTag[R]): Aux[N, R] = ct.asInstanceOf[Aux[N, R]] | ||
// | ||
// def empty[N: AsArray]: Array[N] = Array.empty(clsTag).asInstanceOf[Array[N]] | ||
// | ||
// def apply[N](xs: N*)(implicit ev: AsArray[N]): Array[N] = | ||
// Array(xs.asInstanceOf[Seq[ev.Repr]]: _*)(clsTag).asInstanceOf[Array[N]] | ||
// | ||
// def upcast[R, N](array: Array[R])(implicit ev: Aux[N, R]): Array[N] = | ||
// array.asInstanceOf[Array[N]] | ||
// | ||
// def downcast[N](array: Array[N])(implicit ev: AsArray[N]): Array[ev.Repr] = | ||
// array.asInstanceOf[Array[ev.Repr]] | ||
// } | ||
|
||
trait AsArray[N] { | ||
type Repr | ||
def clsTag: ClassTag[Repr] | ||
|
||
final def empty: Array[N] = Array.empty(clsTag).asInstanceOf[Array[N]] | ||
|
||
final def apply(xs: N*): Array[N] = | ||
Array(xs.asInstanceOf[Seq[Repr]]: _*)(clsTag).asInstanceOf[Array[N]] | ||
|
||
final def upcast[R](array: Array[R]): Array[N] = array.asInstanceOf[Array[N]] | ||
|
||
final def downcast(array: Array[N]): Array[Repr] = array.asInstanceOf[Array[Repr]] | ||
} | ||
|
||
object AsArray { | ||
|
||
type Aux[N, R] = AsArray[N] { type Repr = R } | ||
|
||
private def clsTag[N](implicit ev: AsArray[N]): ClassTag[ev.Repr] = ev.clsTag | ||
|
||
def unsafeDerive[N, R](implicit ct: ClassTag[R]): Aux[N, R] = | ||
new AsArray[N] { | ||
type Repr = R | ||
override def clsTag: ClassTag[Repr] = ct | ||
} | ||
|
||
def empty[N](implicit ev: AsArray[N]): Array[N] = ev.empty | ||
|
||
def apply[N](xs: N*)(implicit ev: AsArray[N]): Array[N] = ev(xs: _*) | ||
|
||
def upcast[R, N](array: Array[R])(implicit ev: Aux[N, R]): Array[N] = ev.upcast(array) | ||
|
||
def downcast[N](array: Array[N])(implicit ev: AsArray[N]): Array[ev.Repr] = ev.downcast(array) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters