scala-macros

    1熱度

    1回答

    我用下面的斯卡拉宏(由this SO question代碼嚴重的啓發)獲得包含在一個給定的包,繼承一個特定性狀的所有對象的列表: object Macros { def allObjects[T <: AnyRef](packageName: String): List[Any] = macro allObjectsImpl[T] def allObjectsImpl[T <

    1熱度

    1回答

    假設我有一個元組 val myTuple: (String,Int,String,...,Boolean) = ("",0,"",..,true) 我可以寫一個類型別名 type MyType = (String,Int,String,...,Boolean) val myTuple: MyType = ("",0,"",..,true) 我可以動態寫這種類型的別名?有沒有一種方法在這種

    5熱度

    2回答

    後 我有下面的宏重新建立類型一致性的最好辦法: def testMacro[T](x: T): Option[T] = macro testMacroImpl[T] def testMacroImpl[T: c.WeakTypeTag](c: Context)(x: c.Expr[T]): c.Expr[Option[T]] = { import c.universe._

    1熱度

    1回答

    假設我有類型A,B,和C,和一個重載的方法: def foo(a: A): C def foo(b: B): C 然後假設我有一個(複雜)的代碼與工作C類型的對象。我想有是採取兩種類型A或B的方法: def bar(x: [A or B]) = { val c = foo(x) // Code that works with c } 當然,我可以寫的bar兩個版本,

    1熱度

    1回答

    def control(x: String): Option[String] = macro controlImpl def controlImpl(c: Context)(x: c.Expr[String]): c.Expr[Option[String]] = { import c.universe._ val result = x.tree match { c

    6熱度

    1回答

    我有以下代碼: object Macros { import scala.language.experimental.macros import scala.reflect.macros.blackbox def hello(): Unit = macro hello_impl def hello_impl(c: blackbox.Context)()

    2熱度

    1回答

    我有一個函數 def grammar(block: => Any): Grammar = macro grammarImpl 與宏實現 def grammarImpl(c: Context)(block: c.Tree): c.Expr[Grammar] = { q"null" } 但我得到一個錯誤,類型不匹配;發現:c.universe.Literal需要:c.Expr [

    6熱度

    2回答

    我在想是否可以在Scala中定義類型如NegativeNumber。這種類型將是一個負數,它將由編譯器同樣進行檢查,以整型,字符串等 val x: NegativeNumber = -34 val y: NegativeNumber = 34 // should not compile 同樣: val s: ContainsHello = "hello world" val s: Cont

    1熱度

    1回答

    我想創建一個Scala宏&在字段的情況下將返回一個getter/setter對,並在方法的情況下部分應用函數。像下面這樣: trait ValRef[T] { def get(): T } trait VarRef[T] extends ValRef[T] { def set(x: T): Unit } // Example: case class Foo()

    1熱度

    2回答

    假設我們的東西的模型,表示爲case class,因爲這樣 case class User(firstName:String,lastName:String,age:Int,planet:Option[Planet]) sealed abstract class Planet case object Earth extends Planet case object Mars extends