Skip to content

Commit

Permalink
Merge pull request #40 from jatcwang/patch-1
Browse files Browse the repository at this point in the history
Add full smart constructor example in readme
  • Loading branch information
carymrobbins authored Jan 8, 2019
2 parents b872cf5 + 6b7f9b8 commit 8f51600
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,17 @@ 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 = ???
}
import io.estatico.newtype.ops._
@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`
Expand Down

0 comments on commit 8f51600

Please sign in to comment.