Created
March 21, 2020 21:06
-
-
Save robhinds/fafacd8efbde8f1ac4eddb7258d02d90 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
sealed trait ValidatorTypeClass[A] { | |
def validate(a: A): Boolean | |
} | |
object ValidatorTypeClass { | |
def validateElement[A](a: A)(implicit v: ValidatorTypeClass[A]) = v.validate(a) | |
implicit def stringElementValidator = new ValidatorTypeClass[String] { | |
override def validate(a: String): Boolean = ??? //validation logic for strings | |
} | |
implicit def numberElementValidator = new ValidatorTypeClass[Double] { | |
override def validate(a: Double): Boolean = ??? //validation logic for numbers | |
} | |
implicit def booleanElementValidator = new ValidatorTypeClass[Boolean] { | |
override def validate(a: Boolean): Boolean = ??? //validation logic for booleans | |
} | |
implicit def complexElementValidator = new ValidatorTypeClass[ComplexElement] { | |
override def validate(a: ComplexElement): Boolean = a.value.forall(validateElement) | |
} | |
} | |
import ValidatorTypeClass._ | |
val complex = ComplexElement( | |
value = List( | |
StringElement(value = "first element"), | |
StringElement(value = "second element") | |
) | |
) | |
validateElement(complex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment