someApi
EitherT
's companion
trait Copyable[T] {
def copy(e: T, string: String): T
}
object Copyable {
implicit def functionCopyable[A, B]: Copyable[A => B] = new Copyable[A => B] {
override def copy(e: A => B, string: String): A => B = new (A => B) {
override def apply(v1: A): B = e(v1)
override def toString(): String = string
}
}
}
object DebugFun {
def apply[T](f: sourcecode.Text[T])(implicit ev: Copyable[T]): T =
ev.copy(f.value, f.source)
}
val fun = DebugFun { x: Int => // error: Identifier expected but integer literal found.
x + 2
}
abstract class Foo(implicit n : sourcecode.Name) {
def getName = n.value
}
val nice = new Foo {}
println(nice.getName)
Prints $anon
nice
new Foo{}
form. Java reflection it is :worried:
class Foo
def foo[N](implicit n : sourcecode.Name.Aux[N], r : Require[SubString[N, 3]=="foo", "variable name must begin with 'foo'"]) : Foo = new Foo
val foo1 = foo() //OK
val goo1 = foo() //Compile-time fail: variable name must begin with 'foo'
2.12.5
macro death - upgrading to 2.12.6 fixed it
@lihaoyi, consider the following:
abstract class Foo(implicit n : sourcecode.Name) {
def getName = n.value
}
val wrapper : Foo = {
println(new Foo{}.getName)
new Foo{}
}
println(wrapper.getName)
This will print
wrapper
wrapper
Is it possible to modify the macro so it would print:
$anon
wrapper
?
MyAppFunL1C20
meaning file MyApp.scala
+ Line 2 + Column 20.
sourcecode
library, so my question is - is it valuable enough to have in a library? or should I try writing my own macro for it?
if (x.name.value.contains("$")) ...
anonfun$1, anonfun$23, anonfun$41
etc, and then using reflection to insatiate classes.