1

Just trying to get my head around the fundamental concepts of Scala. So every time a Class is instantiated with the keyword 'new' we get a new object. A singleton object declared with the object keyword can't be instantiated with the keyword 'new', because there can be only one. In a sense it is instantiated at compile time.

Is 4 a case class of Int? Is 5.07 a case class of Double?

Is 4 an object?

Are classes themselves sometimes, or always objects?

Functions are objects. They are also instances of classes? How does this fit together?

Is Scala as purely object orientated as Smalltalk or has it made some compromises on the OO front?

Rich Oliver
  • 6,001
  • 4
  • 34
  • 57

2 Answers2

9

When you instantiate a class with the new operator, you get a new instance of the class. The new instance is an object.

You can declare a singleton object with the object keyword. Saying that it is instantiated at compile time doesn't really mean anything. Objects only exist while the program runs, not before that time (such as when you are compiling the program). An object is instantiated the first time it is used.

Is 4 a case class of Int? Is 5.07 a case class of Double?

No. 4 and 5.07 are just instances of the classes Int and Double. In Scala they behave in the same way as objects, but behind the scenes 4 and 5.07 are not really objects. To understand this, you have to know about the standard Scala class hierarchy.

At the top of the hierarchy is the type Any. Everything extends Any. Any has two direct subtypes: AnyVal and AnyRef.

  • AnyVal is the supertype of all value types. Value types are the types that map to JVM primitive types (for example: Int -> int, Double -> double etc.).
  • AnyRef is the supertype of all reference types ("regular" objects).

At runtime, everything that extends AnyRef is an object behind the scenes, and everything that extends AnyVal isn't really an object; it maps to a primitive type.

Case classes are just a kind of syntactic sugar. A case class is exactly the same as a normal class, except that the compiler adds some methods automatically for you (which makes them suitable for pattern matching, for example).

Jesper
  • 202,709
  • 46
  • 318
  • 350
3

4 and 5.07 are not objects. They are just instances of Int and Double classes. Look hierarchy here.

Object is not instantiated at compile time. It gets instantiated (in the meaning of object body/constructor execution) when you access to it for the first time.

Functions are not objects too, they are instances of anonymous class that extends FunctionN (e.g. Function2). But yes, there is object, that provides some common utility things, that allow you to write:

//instance of Function2
scala> val sum = (x: Int, y: Int) => x+y
sum: (Int, Int) => Int = <function2>

scala> sum.curried
res0: Int => (Int => Int) = <function1>

// now back to uncurried version with Function object
scala> Function.uncurried(res0)
res1: (Int, Int) => Int = <function2>

If you interested in difference between plain class and case class look here.

Unfortunately, I don't know smalltalk to make assumptions of scala oop purity, compared to smalltalk one.

Community
  • 1
  • 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228