-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #4415: remove show from repl #4437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
9793424
88b62e8
cfcc624
ec958ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,26 +43,29 @@ private[repl] class Rendering(compiler: ReplCompiler, | |
myClassLoader | ||
} | ||
|
||
/** Load the value of the symbol using reflection | ||
/** Load the value of the symbol using reflection. | ||
* | ||
* Calling this method evaluates the expression using reflection | ||
*/ | ||
private[this] def valueOf(sym: Symbol)(implicit ctx: Context): Option[String] = { | ||
val defn = ctx.definitions | ||
val objectName = sym.owner.fullName.encode.toString.dropRight(1) // gotta drop the '$' | ||
val objectName = sym.owner.fullName.encode.toString.stripSuffix("$") | ||
val resObj: Class[_] = Class.forName(objectName, true, classLoader()) | ||
|
||
val res = | ||
val value = | ||
resObj | ||
.getDeclaredMethods.find(_.getName == sym.name.toString + "Show").get | ||
.invoke(null).toString | ||
|
||
.getDeclaredMethods.find(_.getName == sym.name.encode.toString).get | ||
.invoke(null) | ||
val string = value match { | ||
case null => "null" // Calling .toString on null => NPE | ||
case "" => "\"\"" // Sepcial cased for empty string, following scalac | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: Special |
||
case x => x.toString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another special case for |
||
} | ||
if (!sym.is(Flags.Method) && sym.info == defn.UnitType) | ||
None | ||
else if (res.startsWith(str.REPL_SESSION_LINE)) | ||
Some(res.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')) | ||
else if (string.startsWith(str.REPL_SESSION_LINE)) | ||
Some(string.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know the code above is not new, but I'm wondering why this is needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I get it. It is to pretty print a class. However it quickly leaks: scala> class Foo
// defined class Foo
scala> new Foo
val res0: Foo = Foo@7baf1f5a
scala> println(res0.toString)
rs$line$1$Foo@7baf1f5a It is the same in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say it's fine to show the truth in this case, messing up with the object's toString might lead to more confusion... |
||
else | ||
Some(res) | ||
Some(string) | ||
} | ||
|
||
/** Render method definition result */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
scala> val foo = "1"; foo.toInt | ||
val foo: String = "1" | ||
val foo: String = 1 | ||
val res0: Int = 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
scala> val s = "" | ||
val s: String = "" |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import dotty.Show._ | ||
|
||
class Foo(x: Int) | ||
|
||
object Test { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this assume a getter? Do we generate a getter for case like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we're fine:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I guess it doesn't hurt to avoid the .get...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if you could access the field instead