0

As I read in some good Scala books the method declared in Scala REPL are not function(They are not values). They are method.So I want to know exactly which object is consumer that method call. eg when I call println it is syntatic sugar of Predef.println which under the hood call Console.println . So when I define

def sayHello = "Hello" 

then on which object it is being called ,means obj.sayHello what obj is ?

Thanks

optional
  • 3,260
  • 5
  • 26
  • 47

1 Answers1

1

It looks like REPL does the following if you execute a line:

It wraps it into an anonymous object literal, imports the members of all previously generated objects and compiles that. If the line has a return value, it includes a member resX to that object and stores the return value there.

You can see that by executing stuff like this.

scala> def foo = "asd"; this
foo: String
res0: type = @ee7d9f1

scala> res0.foo
res1: String = asd

scala> def foo = this; this
foo: type
res2: type = @6d86b085

scala> res2.foo
res3: type = @6d86b085

scala> res3.foo.res2.foo.foo.res2
res4: type = @6d86b085

The interesting thing is the type of these objects, its "type", and the class of the objects has an empty name. This does not usually happen, maybe it is, what the compiler API generates, if you just give it a line of code that is not wrapped in any context, something, you normally cannot do in Scala.

If you really want to know it exactly, you probably have to look into the source of Scala. But to use REPL it should be enough to know, that your code is wrapped in a fresh anonymous object each time.

dth
  • 2,287
  • 10
  • 17
  • thanks for your reply @dth . I found the answer here :http://stackoverflow.com/questions/7279796/scala-is-there-a-default-class-if-no-class-is-defined/7280136#7280136 – optional Feb 12 '16 at 05:46