Now playing a bit with Scala, I wonder how you should perform input validation in Scala.
This is what I saw many times:
def doSomethingWithPositiveIntegers(i: Int) = {
require(i>0)
//do something
}
to get the hang of it, it looks like it's done in Java:
void doSomething(Object o) {
if (!o instanceof Integer)
throw new IllegalArgumentException();
}
There you first accept more than you are willing to accept, and then introduce some kind of "guard" that allows only the "good." More precisely, you will need these guards in every function that does something with positive integers, and if you want, for example, to enable zero later, you will need to change each function. Of course, you can transfer it to another function, but, nevertheless, you always need to remember that you are calling the correct function, and it may not be able to survive type refactoring, etc. Doesn't sound like I would have this. I was thinking about pushing this verification code to the data type itself, for example:
import scala.util.Try
object MyStuff {
implicit class PositiveInt(val value: Int) {
require(value>0)
}
implicit def positiveInt2Int(positiveInt: PositiveInt): Int = positiveInt.value
}
import MyStuff._
val i: MyStuff.PositiveInt = 5
val j: Int = i+5
println(i)
println(j)
val sum = i + i
println(sum)
def addOne(i: MyStuff.PositiveInt) = i + 1
println(Try(addOne(-5)))
println(Try(addOne(5)))
PositiveInt, , () , Int. , API , - , ! , , , - . - ! , ( URL-, ).
:
:
, , - . , :
val i: PositiveInteger = 5
val range = i to 10
val range = i.value to 10
, Int require, PositiveInt Int ( !), Int :). , , .
. , ( - , ?).
:
- - ? , - , , . , .
- ?
- implicits ( # 1)? , - , (:
PositiveInt RichInt)?