Best way to write a generic method that checks whether a number is 0 in scala -
i know trivial simple case check, seems should possible write method following, generalizes numeric types:
def checknonzero(t: long, field: string): list[string] = { if (t == 0) println("was zero") else println("wasn't zero") } what's best way this?
this can done number type.
def checknonzero(n: number) = n != 0 or can use numeric typeclass.
def checknonzero[t : numeric](n: t) = { val num = implicitly[numeric[t]] !num.equiv(n, num.zero) } edit
actually, can write this:
def checknonzero[t : numeric](n: t) = n != 0 unless define new custom instances of numeric of course.
Comments
Post a Comment