Reputation: 5057
I have the following scala file, test.scala
package p1 {
object ty {
def f() = print ("p1.ty.f")
}
}
package p2 {
object ty extends App {
def f() = println (" in p2.ty.f , in " + p1.ty.f )
// calls above function
ty.f
}
}
when I am trying to run the above code using sbt
(with p2.ty on command line) it outputs the following:
p1.ty.f in p2.ty.f , in ()
while I expect the following :
in p2.ty.f , in p1.ty.f
What causes this behavior - am I missing something?
Upvotes: 0
Views: 3352
Reputation: 26566
p1.ty.f
does not return anything (it actually returns Unit
) so it does not make sense to concatenate the result of this function with another string " in p2.ty.f , in " + p1.ty.f
. So as result, print ("p1.ty.f")
would be executed first and will print p1.ty.f
and then println (" in p2.ty.f , in " + p1.ty.f )
would be executed and will print in p2.ty.f , in ()
because ()
is a string representation of Unit
.
So to achieve the desired result you need to write something like this:
package p1 {
object ty {
def f() = "p1.ty.f"
}
}
package p2 {
object ty extends App {
def f() = println (" in p2.ty.f , in " + p1.ty.f() )
ty.f()
}
}
Upvotes: 1