nrinaudo / scala-best-practices

Collection of best practices for the Scala programming language

Home Page:https://nrinaudo.github.io/scala-best-practices/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What about apply instead of a tricky behaviour (on case class) ?

lamazoni opened this issue · comments

Hello,

It may not be a issue since I m quite (still) a beginner.

About this tricky behaviour on final case class.

It sound like easier to use apply as follow :

final case class PositiveInt2(value: Int)

object PositiveInt2 {
  def apply(i: Int): Option[PositiveInt] =
    if(i > 0) Some(new PositiveInt(i) {})
    else      None
}

PositiveInt2(10)
PositiveInt2(-10)

Let me known if you want a PR on this

sry my bad I forget to replace PositiveInt by PositiveInt2