How does Scala define a type of return value? -
how scala define type of return value?
def sqrt = (x: int) => if (x > 0 && x < 4) x * x the return type code int => anyval. if change to
def method = (x: string) => if (x.equals("abc")) x.concat(x) the return type string => any. why not anyref? string object, better use anyref. wrong?
since if expression has no else, unit returned if condition evaluates false. unit extends anyval int, in first case specific type anyval. since string extends anyref, specific type any in second case.
Comments
Post a Comment