Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent of pythons repr() in scala

Tags:

scala

Is it there an equivalent of Pythons repr function in scala?

Ie a function which you can give any scala object an it will produce a string representation of the object which is valid scala code.

eg:

val l = List(Map(1 -> "a"))

print(repr(l))

Would produce

List(Map(1 -> "a"))
like image 843
Mark Avatar asked Oct 21 '11 14:10

Mark


People also ask

What is repr in Scala?

The inner type, Repr , is what is called a path dependent type in Scala. That is, the type is dependent on the actual instance of the enclosing trait or class. This is a powerful mechanism in Scala.

What is repr () python?

The repr() function returns the string representation of the value passed to eval function by default. For the custom class object, it returns a string enclosed in angle brackets that contains the name and address of the object by default. Example: repr()

Is repr () a python built in function?

Definition. The Python repr() built-in function returns the printable representation of the specified object as a string.

What is def __ repr __( self?

__repr__ (self) Returns a string as a representation of the object. Ideally, the representation should be information-rich and could be used to recreate an object with the same value.


1 Answers

There is mostly only the toString method on every object. (Inherited from Java.) This may or may not result in a parseable representation. In most generic cases it probably won’t; there is no real convention for this as there is in Python but some of the collection classes at least try to. (As long as they are not infinite.)

The point where it breaks down is of course already reached when Strings are involved

"some string".toString == "some string"

however, for a proper representation, one would need

repr("some string") == "\"some string\""

As far as I know there is no such thing in Scala. Some of the serialisation libraries might be of some help for this, though.

like image 63
Debilski Avatar answered Oct 06 '22 02:10

Debilski