Which Object is the consumer for Scala REPL methods? - scala

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

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.

Related

Scala compiler generates wrong method signature for Tuple[Int, _]

Scala compiler behaves weirdly with boxing/unboxing in tuples as parameters.
Consider the code:
scala> class Test { def test(p: (Int, String)) = println(p) }
defined class Test
scala> classOf[Test].getMethods()(0)
res2: java.lang.reflect.Method = public void Test.test(scala.Tuple2)
scala> classOf[Test].getMethods()(0).getGenericParameterTypes
res3: Array[java.lang.reflect.Type] = Array(scala.Tuple2<java.lang.Object, java.lang.String>)
scala> // WTF?????? ^^^^^^^^^^^^^^^^
Thus, I'm getting Object instead of Integer. I assume this is somehow related to tuple parameter being #specialized, but cannot wrap my head around how to avoid/fix this.
The problem it causes - it is impossible to reconstruct method parameter via reflection on method signature (e.g. while parsing json).
Even if there's a way to get the right type with scala-reflect it doesn't help much, cause there are a lot of Java libraries around (like Jersey) that use just Java reflection.
UPD:
OK, putting an Integer (instead of Int) into Tuple works ok. But why isn't it done automatically?

Varargs with different type parameters in scala

I'm new to Scala...
Anyway, I want to do something like:
val bar = new Foo("a" -> List[Int](1), "b" -> List[String]("2"), ...)
bar("a") // gives List[Int] containing 1
bar("b") // gives List[String] containing "2"
The problem when I do:
class Foo(pairs: (String, List[_])*) {
def apply(name: String): List[_] = pairs.toMap(name)
}
pairs is gonna be Array[(String, List[Any]) (or something like that) and apply() is wrong anyway since List[_] is one type instead of "different types". Even if the varargs * returned a tuple I'm still not sure how I'd go about getting bar("a") to return a List[OriginalTypePassedIn]. So is there actually a way of doing this? Scala seems pretty flexible so it feels like there should be some advanced way of doing this.
No.
That's just the nature of static type systems: a method has a fixed return type. It cannot depend on the values of the method's parameters, because the parameters are not known at compile time. Suppose you have bar, which is an instance of Foo, and you don't know anything about how it was instantiated. You call bar("a"). You will get back an instance of the correct type, but since that type isn't determined until runtime, there's no way for a compiler to know it.
Scala does, however, give you a convenient syntax for subtyping Foo:
object bar extends Foo {
val a = List[Int](1)
val b = List[String]("2")
}
This can't be done. Consider this:
val key = readStringFromUser();
val value = bar(key);
what would be the type of value? It would depend on what the user has input. But types are static, they're determined and used at compile time.
So you'll either have to use a fixed number of arguments for which you know their types at compile time, or use a generic vararg and do type casts during runtime.

Scala reflection behaves differently on interpreter and compiled code

I have
trait T
class C extends T
compiled to .class files. And the piece of code below to load them:
val loader = ScalaClassLoader fromURLs (/* List[URL] */)
val classB = loader.tryToInitializeClass("B") getOrElse (/* throw something */)
println(classB.asInstanceOf[Class[_]].getInterfaces)
When I run the loading code in Scala interpreter, the result is
res1: Array[java.lang.Class[_]] = Array(interface T, interface scala.ScalaObject)
but when the loading code compiled into .class files and run I got
[Ljava.lang.Class;#1b8e059
Please tell me how to have the compiled loading code yield the result as fine as on the interpreter.
Are you sure you executed the println in the interpreted session? Because the first result you write looks suspiciously like the interpreter displaying the result of just
classB.asInstanceOf[Class[_]].getInterfaces), without the println (res1 is very telling)
On the other hand, the cryptic [Ljava.lang.Class;#1b8e059 is the toString of an Array. So your problem is just that, toString. If you do something like println(yourResult.mkString(", ")), that should be much better. In the REPL, results displays are better than plain toString
Array(interface T, interface scala.ScalaObject) and [Ljava.lang.Class;#1b8e059 are the same type of object, just printed out in different ways.
Array[Class[_]] gets printed out like [Ljava.lang.Class;#1b8e059 when you call the toString on it.
Try the following:
scala> val f = Array[Class[_]](classOf[Map[String, String]], classOf[Object])
f: Array[java.lang.Class[_]] = Array(interface scala.collection.immutable.Map, class java.lang.Object)
scala> f.toString
res1: java.lang.String = [Ljava.lang.Class;#407e62
The REPL is being helpful when it prints out the value of an expression. If you want to print out a useful string in your compiled code, for example:
scala> f.toList.toString
res4: String = List(interface scala.collection.immutable.Map, class java.lang.Object)
The line
res1: Array[java.lang.Class[_]] = Array(interface T, interface scala.ScalaObject)
does not come from your println expression, it comes from the REPL. If an entered expression returns anything except Unit. The REPL prints the name, type and result of the toString method of that object.
name: Type = from toString

var_dump() in Scala

Is there any convenient way to dump all members of a specified object in Scala,
like var_dump(), PHP function?
As mentioned in "How to Dump/Inspect Object or Variable in Java" (yes, I know, the question is about Scala):
Scala (console) has a very useful feature to inspect or dump variables / object values :
scala> def b = Map("name" -> "Yudha", "age" -> 27)
b: scala.collection.immutable.Map[java.lang.String,Any]
scala> b
res1: scala.collection.immutable.Map[java.lang.String,Any] = Map((name,Yudha), (age,27))
But if you want more details, you can give REPL Scala Utils a try, in order to get a "Easier object inspection in the Scala REPL"
So I've written a utility for use on the Scala REPL that will print out all of the "attributes" of an object.
(Note: "I" being here: Erik Engbrecht, also on BitBucket)
Here's some sample usage:
scala> import replutils._
import replutils._
scala> case class Test(a: CharSequence, b: Int)
defined class Test
scala> val t = Test("hello", 1)
t: Test = Test(hello,1)
scala> printAttrValues(t)
hashCode: int = -229308731
b: int = 1
a: CharSequence (String) = hello
productArity: int = 2
getClass: Class = class line0$object$$iw$$iw$Test
That looks fairly anti-climatic, but after spending hours typing objName to see what's there, and poking at methods, it seems like a miracle.
Also, one neat feature of it is that if the class of the object returned is different from the class declared on the method, it prints both the declared class and the actual returned class.
You might want to look at ToStringBuilder in commons-lang, specificly ToStringBuilder.reflectionToString().
In compiled code, the nicest way is usually just to declare your type as a case class, then use the generated toString method.
Anything else subclassing Product should be just as easy (currently just tuples)
Failing that, write your own toString method, it's usually trivial enough...

What does it mean when I use def to define a field in Scala?

What exactly is the difference between:
scala> def foo = 5
foo: Int
and
scala> def foo() = 5
foo: ()Int
Seems that in both cases, I end up with a variable foo which I can refer to without parenthesis, always evaluating to 5.
You're not defining a variable in either case. You're defining a method. The first method has no parameter lists, the second has one parameter list, which is empty. The first of these should be
called like this
val x = foo
while the second should be called like this
val x = foo()
However, the Scala compiler will let you call methods with one empty parameter list without the parentheses, so either form of call will work for the second method. Methods without parameter lists cannot be called with the parentheses
The preferred Scala style is to define and call no-argument methods which have side-effects with the parentheses. No-argument methods without side-effects should be defined and called without the parentheseses.
If you actually which to define a variable, the syntax is
val foo = 5
Before anything else is said, def does not define a field, it defines a method.
In the second case, you can omit parenthesis because of a specific feature of Scala. There are two differences of interest here: one mechanical, and one of recommended usage.
Beginning with the latter, it is recommended usage to use empty parameter list when there are side effects. One classic example is close(). You'd omit parenthesis if there are no side effects to calling the element.
Now, as a practical difference -- beyond possible weird syntactic mix-ups in corner cases (I'm not saying there are, just conjecturing) -- structural types must follow the correct convention.
For example, Source had a close method without parenthesis, meaning a structural type of def close(): Unit would not accept Source. Likewise, if I define a structural method as def close: Unit, then Java closeable objects will not be accepted.
What does it mean when I use def to define a field in Scala
You can't define a field using def.
Seems that in both cases, I end up with a variable foo which I can refer to without parenthesis, always evaluating to 5.
No, in both cases you end up with a method foo, which you can call without parentheses.
To see that, you can use javap:
// Main.scala
object Main {
def foo1 = 5
def foo2() = 5
}
F:\MyProgramming\raw>scalac main.scala
F:\MyProgramming\raw>javap Main
Compiled from "main.scala"
public final class Main extends java.lang.Object{
public static final int foo2();
public static final int foo1();
}
However, see http://tommy.chheng.com/index.php/2010/03/when-to-call-methods-with-or-without-parentheses-in-scala/
Additionally to the answers already given I'd like to stress two points:
The possibility to define methods without a parameter list is a way to realize the Uniform Access Principle. This allows to hide the difference between fields and methods, which makes later implementation changes easier.
You can call a method defined as def foo() = 5 using foo, but you can't call a method defined as def foo = 5 using foo()
I'm surprised that nobody mentioned anything about the laziness difference.
While val is evaluated only once at the time of definition, def is evaluated only when we access it and evaluated every-time we access it. See example below:
scala> def foo = {
| println("hi")
| 5
| }
foo: Int
scala> val onlyOnce = foo
scala> def everyTime = foo
scala> onlyOnce
res0: Int = 5
scala> onlyOnce
res1: Int = 5
scala> everyTime
hi
res2: Int = 5
scala> everyTime
hi
res3: Int = 5