score:129

Accepted answer

i'll disagree with chris's answer in one regard. the classes any, anyref and anyval are classes. but they don't appear as classes in bytecode, because of intrinsic limitations of the jvm.

this arises out of the fact that not everything in java is an object. in addition to objects, there are primitives. all objects in java are descendant from java.lang.object, but primitives are set apart and, presently*, not extensible by a programmer. note also that primitives have "operators", not methods.

in scala, on the other hand, everything is an object, all objects belong to a class, and they interact through methods. the jvm bytecode generated does not reflect this, but that doesn't make them any less so, just as java has generics, even though the bytecode doesn't have them.

so, in scala, all objects are descendant from any, and that includes both what java considers objects and what java considers primitives. there's no equivalent in java because there is no such unification.

everything that is considered a primitive in java is descendant from anyval in scala. until scala 2.10.0, anyval was sealed, and programmers were unable to extend it. it should be interesting to see what will happen with scala on .net, since interoperability alone calls for scala to at least recognize user-defined "primitives".

also extending any is anyref, which is equivalent to java.lang.object (on the jvm at any rate).

up to scala 2.9.x, a user could not extend any or anyval, nor reference them from java, but there were other uses they could be put to in scala. specifically, type signatures:

def f(x: anyval) = println(x)
def g(x: anyref) = println(x)
def h(x: any) = println(x)

what each means should be obvious from the class hierarchy. of note, however, is that f and h will auto-box, but g will not. that is a bit the opposite of what java does, in that f and h cannot be specified, and g (defined with java.lang.object) would cause auto-boxing.

starting with scala 2.10.0, though, the user can extend anyval or any, with the following semantics:

  • if a class extends anyval, no instance will be created for it on the heap under certain conditions. this means the fields of this class (on 2.10.0 only a single field is allowed -- whether that will change remains to be seen) will stay on the stack, whether they are primitives or references to other objects. this allows extension methods without the instantiation cost.

  • if a trait extends any, then it can be used with both classes that extend anyref and classes that extend anyval.

ps: in my own view, java is likely to follow c# in allowing "struct" primitives, and maybe typedefs, as parallelism without resorting to them is proving difficult to accomplish with good performance.

score:5

any and anyval are, i believe, part of the scala type system and are not classes as such (in the same way that nothing is a type, not a class). you cannot use them explicitly from within java code.

howwever, in java/scala interoperation, a method which accepts a java object will expect a scala any / anyref.

what are you actually attempting to do?

score:44

seen this? the text of the page has some java interoperability remarks. http://www.scala-lang.org/node/128

scala class heierarchy


Related Query

More Query from same tag