Skip to content

Commit

Permalink
Updates to README for PR #40
Browse files Browse the repository at this point in the history
  • Loading branch information
carymrobbins committed Jan 8, 2019
1 parent 8f51600 commit 266f7cb
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,29 @@ prevent this by defining the field as private.
Using `class` will not generate a smart constructor (no `apply` method). This allows
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 {
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`
just as if you were dealing with a normal class.

```scala
@newtype class N(val a: A)
```

If you need to define your own smart constructor, use the
`.coerce` extension method to cast to your newtype.

```scala
import io.estatico.newtype.ops._

@newtype class Id(val strValue: String)

object Id {
def fromString(str: String): Either[String, Id] = {
if (str.isEmpty) Left("Id cannot be empty")
else Right(str.coerce)
}
}
```

#### Extension Methods

Defining extension methods are as simple as defining normal methods in any class -
Expand Down

0 comments on commit 266f7cb

Please sign in to comment.