According to this, Scala methods belong to a class. However, if I define a method in REPL or in a script that I then execute using scala, what class does the method belong to ?
scala> def hoho(str:String) = {println("hoho " + str)}
hoho: (str: String)Unit
scala> hoho("rahul")
hoho rahul
In this example, what class does the method belong to ?
The REPL wraps all your statements (actually rewrites your statements) in objects automagically. You can see it in action if you print the intermediate code by using the
-Xprint:typeroption:So your method
hohois really$line1.$read.$iw.$iw.hoho. Then when you usehoho("foo")later on, it’ll rewrite to add the package and outer objects.Additional notes: for scripts,
-Xprint:typer(-Xprint:parser) reveals that the code is wrapped inside a code block in themain(args:Array[String])of an objectMain. You have access to the arguments asargsorargv.