Scala String vs java.lang.String - type inference -
in repl, define function. note return type.
scala> def next(i: list[string]) = i.map {"0" + _} ::: i.reverse.map {"1" + _} next: (i: list[string])list[java.lang.string] and if specify return type string
scala> def next(i: list[string]): list[string] = i.map {"0" + _} ::: i.reverse.map {"1" + _} next: (i: list[string])list[string] why difference? can specify return type list[any], guess string wrapper supertype java.lang.string. have practical implications or can safely not specify return type?
this question! first, let me assure you can safely specify return type.
now, let's it... yes, when left inference, scala infers java.lang.string, instead of string. so, if "string" in scaladoc, won't find anything, seems indicate not scala class either. well, has come someplace, though.
let's consider scala imports default. can find on repl:
scala> :imports 1) import java.lang._ (155 types, 160 terms) 2) import scala._ (801 types, 809 terms) 3) import scala.predef._ (16 types, 167 terms, 96 implicit) the first 2 packages -- and, indeed, string can found on java.lang! it, then? let's check instantiating else package:
scala> val s: stringbuffer = new stringbuffer s: java.lang.stringbuffer = scala> val s: string = new string s: string = "" so, doesn't seem it. now, can't inside scala package, or have been found when looking on scaladoc. let's inside scala.predef, , there is!
type string = string that means string alias java.lang.string (which imported previously). looks cyclic reference though, if check source, you'll see defined full path:
type string = java.lang.string next, might want ask why? well, don't have idea, suspect make such important class little less dependent on jvm.
Comments
Post a Comment