From c7bb2a8cef18376a3237a3a8670230f69e444fc9 Mon Sep 17 00:00:00 2001 From: jatcwang Date: Mon, 5 Nov 2018 18:07:01 +0000 Subject: [PATCH 1/2] Improve smart constructor example --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 04a628f..9eff235 100644 --- a/README.md +++ b/README.md @@ -165,10 +165,16 @@ Using `class` will not generate a smart constructor (no `apply` method). This al you to specify your own. Note that `new` never works for newtypes and will fail to compile. ```scala -@newtype class N(a: A) -object N { - def apply(a: A): N = ??? -} + @newtype class Id(val strValue: String) + + object Id { + def apply(str: String): Either[String, Id] = { + if (str.isEmpty) + Left("Id cannot be empty") + else + Right(str.coerce) + } + } ``` If you wish to generate an accessor method for your underlying value, you can define it as `val` From 6b7f9b8c00c82380d5299c891929872e0b5ffd85 Mon Sep 17 00:00:00 2001 From: jatcwang Date: Mon, 5 Nov 2018 18:09:36 +0000 Subject: [PATCH 2/2] add import in example --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9eff235..3a973de 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ Using `class` will not generate a smart constructor (no `apply` method). This al you to specify your own. Note that `new` never works for newtypes and will fail to compile. ```scala + import io.estatico.newtype.ops._ @newtype class Id(val strValue: String) object Id {