var_dump() in Scala - 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...

Related

Which Object is the consumer for Scala REPL methods?

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.

In Scala what is the correct way to fix "error: forward reference extends over definition of value"?

In Scala what is the correct way to implement something like this that produces the error "forward reference extends over definition of value b"?
object a {
def main(args: Array[String]) {
val b: Map[Int, () => Int] = Map(5 -> { () => b.size })
println(b(5)())
}
}
Making b lazy works but that doesn't seem like the correct solution.
EDIT: The other question (What does "Forward reference extends over definition of value" mean in Scala?) is about a bug in Scala itself that where this error is reported incorrectly (or that is what the accepted answer suggests). This question is about what to do when this error is reported correctly. Also I supplied sample code.
Making b lazy seems like overkill because it changes the run-time behavior, when this is a compile-time problem that I assume could be fixed with a different declaration...
As mentioned in the comments, making b a def works too, which seems closer to the solution since def works for recursive functions, but that seems to be reevaluating it at run-time every time it is accessed.
scala> var a = 5
scala> def b = a
scala> a = 2
scala> b
res0: Int = 2
scala> a = 1
scala> b
res1: Int = 1
(with lazy it can be changed before it is accessed but is stored after that)
Both of these seem like run-time solutions to a compile-time problem, I'm looking for something analogous to letrec in Scala.
When you define a non-lazy val, you cannot refer to that val inside its definition. For that you either have to use a def or a lazy val.

Trouble with constructors in Scala

It's been a while since I've used constructors at all- so naturally when I have to use one in Scala I'm having trouble.
I want to do the following: When I create a new class without passing through anything- it creates an empty vector.
Otherwise if it passes through a vector- we use that vector and define it to be used with the class.
How do I do this? I previously had
Class example{
val a: Vector[int] = Vector();
Then I'm lost. I was thinking of doing something like
Class example{
val a: Vector[Int] = Vector()
def this(vector: Vector[Int]){
this{
a = vector
}
}
But I'm getting tons of errors. Can anyone help? I'm trying to find my scala book but I can't find it- I know it had a good section on constructors.
Sounds like you want a constructor with a default argument:
class example(val a : Vector[Int] = Vector())
If you really want to do this by constructor overloading, it looks like this:
class Example(val a: Vector[Int]) {
def this() = this(Vector())
}
Personal-opinion addendum: Overloading and default arguments are often good to avoid. I'd recommend just making a different function that calls the constructor:
class Example(val a: Vector[Int])
object Example {
def empty = new Example(Vector())
}
case class Example(a: Vector[Int] = Vector())
No need to put the val keyword.
Also, using the case keywork you get:
a compact initialisation syntax: Example(Vector(1,2)), instead of new Example(Vector(1,2))
pattern matching for you class
equality comparisons implicitly defined and pretty toString
Reference

How do I get Scala's Option class so I can pass it to getDeclaredMethod()

I'm trying to get the classOf[the-abstract-class-Option], but instead I always get the classOf[the-Option-*object*]. How can I get the class of the abstract class instead?
Both Option.getClass and classOf[Option[_]] gives me class scala.Option$.
Edit: I needn't have asked this; all of a sudden, classOf[Option[_]] works fine, weird. /Edit
Background:
I'm trying to invoke via reflection a method that takes an Option[String] parameter.
It signature look like so: ...(..., anySectionId: Option[String], ...)...
Before I can invoke the method, I look it up via getDeclaredMethod. But to do that, I need a list of parameter types, which I construct by calling _.getClass on each argument I'm going to give to the method. But _.getClass returns classOf[None] or classOf[Some] for Option instances, which makes getDeclaredMethod fail, because (?) the signature is based on Option not Some/None.
Here's the code:
val clazz: Class[_] = Play.current.classloader.loadClass(className)
val paramTypes = arguments.map(_ match {
case None => Option.getClass // gives me the object, not the abstract class
case _: Some[_] => classOf[Option[_]] // this also gives me the object :-(
case x => x.getClass // results in None or Some for Option instances
})
val m: jl.reflect.Method = clazz.getDeclaredMethod("apply", paramTypes: _*)
and the last line above fails for a method with any Option parameter (otherwise everything works fine).
The best way is use Scala reflection.
The next best way is not to make work for yourself by trying to match the param types.
Using getClass fails for subtypes:
scala> class Foo
defined class Foo
scala> class Bar extends Foo
defined class Bar
scala> class Baz { def baz(f: Foo) = 1 }
defined class Baz
scala> val b = new Baz
b: Baz = Baz#d33eaa9
scala> val p = new Bar
p: Bar = Bar#406c5ca2
scala> classOf[Baz].getDeclaredMethod("baz", p.getClass)
java.lang.NoSuchMethodException: Baz.baz(Bar)
It's easier just to match on the name:
scala> classOf[Baz].getMethods.find(_.getName == "baz") map (_.invoke(b,p)) getOrElse -1
res5: Any = 1
or filter on the number of params for poor man's overloading resolution, then maybe filter on all args having conforming types.
The notation for accidentally getting the object is in fact:
scala> classOf[Option$]
res8: Class[Option$] = class scala.Option$
Answer: classOf[Option[_]]
Weird! Suddenly classOf[Option[_]] works. I feel sure I tested once or twice before I submitted the question :-( Perhaps the IDE didn't have time to save the file before I recompiled, weird.
I don't know if I should delete the question. Or perhaps I should leave it as is, in case classOf[Option[_]] isn't obvious to everyone.

Overloading the existing `toInt` method

The toInt method in StringLike doesn't take any arguments, and can only parse in decimal. So to parse binary, hex etc we need to resort to Java's Integer#parseInt(String s, int radix).
In an attempt to remedy this state of affairs, I tried the following
implicit def strToToIntable(s: String) = new {
def toInt(n: Int) = Integer.parseInt(s, n)
}
However,
"101".toInt(2)
causes the REPL compiler to "crash spectacularly" and doesn't work in compiled code either.
Is there some restriction on overloading existing methods using the "enrich my library" pattern?
Without the implicit, running "101".toInt(2) causes REPL to tell me that Int does not take parameters. So I guess what is happening is that it's running "101".toInt, then trying to call apply(2) on that, which doesn't make sense. I'd suggest a subtle rename of your pimped toInt to avoid the problem.
edit
I just had some success of my own. I explicitly defined a pimped string class as
class StrToRadixInt(s:String) {
def toInt(radix: Int) = Integer.parseInt(s,radix)
}
implicit def strToToIntable(s:String) = new StrToRadixInt(s)
And REPL was happy:
scala> "101".toInt(2)
res4: Int = 5
The REPL shouldn't crash--that's a bug. But even so, overloading of names is somewhat discouraged and also not supported in some contexts. Just use a different name:
implicit def parseBase(s: String) = new { def base(b: Int) = Integer.parseInt(s,b) }
scala> "10110" base 2
res1: Int = 22