Andrés Testi | 29 Dec 04:00
Picon

Re: Scala Object Notation ( SCON? )

These are my reasons to refuse anonymous subclasses:

1) Subclassing breaks the encapsulation contracts allowing side
effects. For example, the Person class have a protected var:

class Person{
   var name: String = _
   protected var internalCode: Int = _
}

val p = new Person{
  name = "Andres" // Good
  internalCode = 123432 // DANGER!!!! I'just want to instantiate a
class with the public vars
}

2) Hiding the owner scope:

class Person{
  protected def op(a: Int) = a + 1
}

def op(a: Int) = a - 1

val p = new  Person{
  age = op(17) // WARNING!!!
}

3) Anonymous class != Person class:
(new Person).getClass != (new Person{name="Andres"}).getClass

4) This is just a function invocation
new Person(name: "Andres", age: 29)

this is a code secuence
new Person{name = "Andres"; age = 29 }

The JSON-style constructor don't breaks client contracts, that just
instantiates a Person and initializes public vars. I can survive
without this, but I prefer more defensive and declarative programming.

2007/12/28, martin odersky <martin.odersky <at> epfl.ch>:
> >
> > class Person{
> >  String name;
> >  Integer age;
> > }
> >
> > p = new Person(name: "Andres", age: 29)
> >
> > A pretty feature to add to Scala.
>
> I (still) don't understand. The above feature already works one by one in Scala:
>
>   class Person {
>      var name: String = _
>      var age: Int = _
>    }
>    val p = new Person{ name = "Andres", age = 29 }
>
> Cheers
>
> -- Martin
>


Gmane