forked from zio/zio-prelude
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zio#794: Derivation tools for external newtypes
- Loading branch information
1 parent
3a27000
commit 565ceab
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
core-tests/shared/src/test/scala/zio/prelude/newtypes/DerivationSpec.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,43 @@ | ||
package zio.prelude.newtypes | ||
|
||
import zio.prelude.newtypes.derivation.derive | ||
import zio.prelude.{Newtype, Subtype} | ||
import zio.test._ | ||
|
||
object DerivationSpec extends DefaultRunnableSpec { | ||
trait Show[A] { | ||
def show(a: A): String | ||
} | ||
|
||
implicit val showString: Show[String] = (a: String) => a | ||
|
||
// these are imported from an external library - we have no control over them | ||
object External { | ||
object Secret extends Newtype[String] | ||
type Secret = Secret.Type | ||
|
||
object Password extends Subtype[String] | ||
type Password = Password.Type | ||
} | ||
|
||
import External._ | ||
|
||
override def spec: ZSpec[Environment, Failure] = suite("external derivation for newtypes")( | ||
test("works for Newtype") { | ||
val showSecret: Show[Secret] = derive | ||
assertTrue(showSecret.show(Secret("foo")) == "foo") | ||
}, | ||
test("works for Subtype") { | ||
val showPassword: Show[Password] = derive | ||
assertTrue(showPassword.show(Password("foo")) == "foo") | ||
}, | ||
testM("works with auto derivation") { | ||
typeCheck(""" | ||
|import zio.prelude.newtypes.derivation.auto._ | ||
|implicitly[Show[Secret]] | ||
|implicitly[Show[Password]] | ||
|""".stripMargin).absolve | ||
.as(assertCompletes) | ||
} | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
core/shared/src/main/scala/zio/prelude/newtypes/derivation.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,14 @@ | ||
package zio.prelude.newtypes | ||
|
||
import zio.prelude.Newtype | ||
|
||
object derivation { | ||
def derive[A, STA <: Newtype[A], F[_]](implicit ev: STA <:< Newtype[A], typeClass: F[A]): F[STA#Type] = | ||
typeClass.asInstanceOf[F[STA#Type]] | ||
|
||
trait Auto { | ||
implicit def auto[A, STA <: Newtype[A], F[_]](implicit ev: STA <:< Newtype[A], typeClass: F[A]): F[STA#Type] = | ||
derive | ||
} | ||
object auto extends Auto | ||
} |