-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Introduce ReplPrinter as a hook for alternatives to replStringOf #5222
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 2 commits
c3969fb
bcf91b7
9533e80
d571c52
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package scala.tools.nsc | ||
package interpreter | ||
|
||
import scala.runtime.ScalaRunTime | ||
|
||
trait ReplPrinter[-A] extends Any { | ||
def print(x: A, maxElements: Int): String | ||
} | ||
|
||
object ReplPrinter { | ||
implicit def default[A]: ReplPrinter[A] = new ReplPrinter[A] { | ||
def print(x: A, maxElements: Int) = ScalaRunTime.replStringOf(x, maxElements) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,17 @@ repl-out-dir-run.obj | |
$eval$.class | ||
$eval.class | ||
$line2 | ||
$eval$$iw$$iw$.class | ||
$eval$$iw$.class | ||
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. does this mean every repl line generates two additional classfiles? is there a drawback to this? (performance?) 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. Yeah it does. I'm not sure what drawbacks there might be, but I avoided it for Spark-reasons. |
||
$eval$.class | ||
$eval.class | ||
$read$$iw$$iw$.class | ||
$read$$iw$.class | ||
$read$.class | ||
$read.class | ||
$line3 | ||
$eval$$iw$$iw$.class | ||
$eval$$iw$.class | ||
$eval$.class | ||
$eval.class | ||
$read$$iw$$iw$.class | ||
|
@@ -27,13 +31,17 @@ repl-out-dir-run.obj | |
$read$.class | ||
$read.class | ||
$line4 | ||
$eval$$iw$$iw$.class | ||
$eval$$iw$.class | ||
$eval$.class | ||
$eval.class | ||
$read$$iw$$iw$.class | ||
$read$$iw$.class | ||
$read$.class | ||
$read.class | ||
$line5 | ||
$eval$$iw$$iw$.class | ||
$eval$$iw$.class | ||
$eval$.class | ||
$eval.class | ||
$read$$iw$$iw$.class | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
scala> :paste < EOF | ||
// Entering paste mode (EOF to finish) | ||
|
||
trait Show[-A] extends Any { def show(x: A): String } | ||
object Show { | ||
implicit def shouty[Any]: Show[Any] = | ||
new Show[Any] { def show(x: Any) = if (x == null) "" else x.toString + "!" } | ||
} | ||
EOF | ||
|
||
// Exiting paste mode, now interpreting. | ||
|
||
defined trait Show | ||
defined object Show | ||
|
||
scala> | ||
|
||
scala> implicitly[Show[Int]] show 23 | ||
res0: String = 23! | ||
|
||
scala> | ||
|
||
scala> import scala.tools.nsc.interpreter.ReplPrinter | ||
import scala.tools.nsc.interpreter.ReplPrinter | ||
|
||
scala> implicit def showReplPrinter[A](implicit z: Show[A]): ReplPrinter[A] = new ReplPrinter[A] { | ||
def print(x: A, maxElements: Int): String = { | ||
val s = z show x | ||
val nl = if (s contains "\n") "\n" else "" | ||
nl + s + "\n" | ||
} | ||
} | ||
showReplPrinter: [A](implicit z: Show[A])scala.tools.nsc.interpreter.ReplPrinter[A] | ||
|
||
scala> | ||
|
||
scala> 23 | ||
res1: Int = 23 | ||
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 was expecting a "!" at the end here -- how come there's not? 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. Oh, this is the class-based one. 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. perhaps add a comment to the test case that says we don't enable custom string of under this mode 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. Everyone expects it to end with a bang. 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 wasn't sure how to leave the comment, so I've opted for in the transcript. 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. This comment didn't fold because that line it's on didn't change, so, for clarity, I've added a commit that adds the comment. |
||
|
||
scala> :quit |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
object Test extends scala.tools.partest.ReplTest { | ||
override def transformSettings(settings: scala.tools.nsc.Settings) = { | ||
settings.Yreplclassbased.value = true | ||
settings | ||
} | ||
|
||
def code = """ | ||
:paste < EOF | ||
trait Show[-A] extends Any { def show(x: A): String } | ||
object Show { | ||
implicit def shouty[Any]: Show[Any] = | ||
new Show[Any] { def show(x: Any) = if (x == null) "" else x.toString + "!" } | ||
} | ||
EOF | ||
|
||
implicitly[Show[Int]] show 23 | ||
|
||
import scala.tools.nsc.interpreter.ReplPrinter | ||
implicit def showReplPrinter[A](implicit z: Show[A]): ReplPrinter[A] = new ReplPrinter[A] { | ||
def print(x: A, maxElements: Int): String = { | ||
val s = z show x | ||
val nl = if (s contains "\n") "\n" else "" | ||
nl + s + "\n" | ||
} | ||
} | ||
|
||
23 | ||
""" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
scala> :paste < EOF | ||
// Entering paste mode (EOF to finish) | ||
|
||
trait Show[-A] extends Any { def show(x: A): String } | ||
object Show { | ||
implicit def shouty[Any]: Show[Any] = | ||
new Show[Any] { def show(x: Any) = if (x == null) "" else x.toString + "!" } | ||
} | ||
EOF | ||
|
||
// Exiting paste mode, now interpreting. | ||
|
||
defined trait Show | ||
defined object Show | ||
|
||
scala> | ||
|
||
scala> implicitly[Show[Int]] show 23 | ||
res0: String = 23! | ||
|
||
scala> | ||
|
||
scala> import scala.tools.nsc.interpreter.ReplPrinter | ||
import scala.tools.nsc.interpreter.ReplPrinter | ||
|
||
scala> implicit def showReplPrinter[A](implicit z: Show[A]): ReplPrinter[A] = new ReplPrinter[A] { | ||
def print(x: A, maxElements: Int): String = { | ||
val s = z show x | ||
val nl = if (s contains "\n") "\n" else "" | ||
nl + s + "\n" | ||
} | ||
} | ||
showReplPrinter: [A](implicit z: Show[A])scala.tools.nsc.interpreter.ReplPrinter[A] | ||
|
||
scala> | ||
|
||
scala> 23 | ||
res1: Int = 23! | ||
|
||
scala> :quit |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
object Test extends scala.tools.partest.ReplTest { | ||
def code = """ | ||
:paste < EOF | ||
trait Show[-A] extends Any { def show(x: A): String } | ||
object Show { | ||
implicit def shouty[Any]: Show[Any] = | ||
new Show[Any] { def show(x: Any) = if (x == null) "" else x.toString + "!" } | ||
} | ||
EOF | ||
|
||
implicitly[Show[Int]] show 23 | ||
|
||
import scala.tools.nsc.interpreter.ReplPrinter | ||
implicit def showReplPrinter[A](implicit z: Show[A]): ReplPrinter[A] = new ReplPrinter[A] { | ||
def print(x: A, maxElements: Int): String = { | ||
val s = z show x | ||
val nl = if (s contains "\n") "\n" else "" | ||
nl + s + "\n" | ||
} | ||
} | ||
|
||
23 | ||
""" | ||
} |
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.
Could we have one instance, like so:
?
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.
Yep, good eye. Done.