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

Popular posts from this blog

rdbms - what exactly the undo information lives in oracle? -

bash - How do you programmatically add a bats test? -

clojure - 'get' replacement that throws exception on not found? -