Then clearly, toString() has a "side effect". But I am saying that, since I don't know how toString() was implemented unless I go through the source code for Foo, I am just going to use "toString" as in:
a.toString
val a = new Foo
1. toString vs toString()
2. primary constructor without parameters.
Actually, you can call "toString" in either call sites and it works fine. This is specified in Programming in Scala, 2nd Edition
Could you explain what you mean?
Ah, I see. This method is defined by java, so it's toString() even if you override it with parens. Try some other method.
On Tue, Sep 4, 2012 at 10:32 PM, George Li <glu...@gmail.com> wrote:Could you explain what you mean?I had this crazy notion we had uniform access... I apologize. I was thinking of this, and now I'm not sure if I should be happy or sad that the nullary method and the val have different behavior.class A { override val toString = "foo" }
Here are some non-toString situations to consider.class C { def foo: () => Int = () => 1 }class D extends C { override def foo(): () => Int = () => 2 }
scala> (new D).foo()res1: () => Int = <function0>
scala> (new C).foo()res4: Int = 1
ok, I made small test
scala> class Foo{def hello = "hello foo"}
defined class Foo
scala> (new Foo).hello()
<console>:9: error: not enough arguments for method apply: (index: Int)Char in class StringOps.
Unspecified value parameter index.
(new Foo).hello()
^
This difference of behavior is not unsound (given the example below) so it technically does not violate the Liskov substitution principle, but it still feels bad enough that this overloading should be disallowed - especially since D.foo is supposed to override C.foo (and it does). WDYT?