dotty

How to access parameter list of case class in a dotty macro

南笙酒味 提交于 2020-07-18 06:31:05
问题 I am trying to learn meta-programming in dotty. Specifically compile time code generation. I thought learning by building something would be a good approach. So I decided to make a CSV parser which will parse lines into case classes. I want to use dotty macros to generate decoders trait Decoder[T]{ def decode(str:String):Either[ParseError, T] } object Decoder { inline given stringDec as Decoder[String] = new Decoder[String] { override def decode(str: String): Either[ParseError, String] =

How does given keyword work in Scala 3 or dotty?

試著忘記壹切 提交于 2020-06-16 11:22:12
问题 I was going through Scala 3 documentation. They have introduced given keyword which is considered as the alternative of Scala 2 implicit . The code is here trait Ord[T] { def compare(x: T, y: T): Int def (x: T) < (y: T) = compare(x, y) < 0 def (x: T) > (y: T) = compare(x, y) > 0 } given intOrd: Ord[Int] { def compare(x: Int, y: Int) = if (x < y) -1 else if (x > y) +1 else 0 } given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] { def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match { case

How does given keyword work in Scala 3 or dotty?

白昼怎懂夜的黑 提交于 2020-06-16 11:21:42
问题 I was going through Scala 3 documentation. They have introduced given keyword which is considered as the alternative of Scala 2 implicit . The code is here trait Ord[T] { def compare(x: T, y: T): Int def (x: T) < (y: T) = compare(x, y) < 0 def (x: T) > (y: T) = compare(x, y) > 0 } given intOrd: Ord[Int] { def compare(x: Int, y: Int) = if (x < y) -1 else if (x > y) +1 else 0 } given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] { def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match { case

How to use given in Dotty?

依然范特西╮ 提交于 2020-06-12 07:35:25
问题 I was looking at Dotty docs under Contextual Abstractions page and I saw the Given Instances . Given instances (or, simply, "givens") define "canonical" values of certain types that serve for synthesizing arguments to given clauses. Example: trait Ord[T] { def compare(x: T, y: T): Int def (x: T) < (y: T) = compare(x, y) < 0 def (x: T) > (y: T) = compare(x, y) > 0 } given intOrd: Ord[Int] { def compare(x: Int, y: Int) = if (x < y) -1 else if (x > y) +1 else 0 } given listOrd[T]: (ord: Ord[T])

Type inference changes in Scala 3

旧巷老猫 提交于 2020-06-11 05:08:27
问题 What changes in type inference will Scala 3 bring? Currently documentation simply states TODO. For example, Weak conformance Scala 2.13 scala> val i: Int = 42 val i: Int = 42 scala> val c: Char = 'a' val c: Char = a scala> List(i,c) val res0: List[Int] = List(42, 97) Scala 3 (dotty 0.24.0-RC1) scala> val i: Int = 42 val i: Int = 42 scala> val c: Char = 'a' val c: Char = a scala> List(i,c) val res0: List[AnyVal] = List(42, a) Equality Scala 2.13 scala> 42 == Some(42) ^ warning: comparing

Type inference changes in Scala 3

◇◆丶佛笑我妖孽 提交于 2020-06-11 05:04:11
问题 What changes in type inference will Scala 3 bring? Currently documentation simply states TODO. For example, Weak conformance Scala 2.13 scala> val i: Int = 42 val i: Int = 42 scala> val c: Char = 'a' val c: Char = a scala> List(i,c) val res0: List[Int] = List(42, 97) Scala 3 (dotty 0.24.0-RC1) scala> val i: Int = 42 val i: Int = 42 scala> val c: Char = 'a' val c: Char = a scala> List(i,c) val res0: List[AnyVal] = List(42, a) Equality Scala 2.13 scala> 42 == Some(42) ^ warning: comparing

神奇的Scala Macro之旅(二)- 一个实例

大兔子大兔子 提交于 2020-05-02 10:23:44
优化的日志方式 package macros_demo import scala.language.experimental.macros import org.slf4j._ import scala.reflect.macros.whitebox. Context object Macros { implicit class LoggerEx( val logger: Logger) { def DEBUG( msg: String) : Unit = macro LogMacros. DEBUG1 def DEBUG( msg: String, exception: Exception) : Unit = macro LogMacros. DEBUG2 } object LogMacros { def DEBUG1( c: Context)( msg: c. Tree) : c. Tree = { import c.universe._ val pre = c.prefix q """ val x = $pre.logger if( x.isDebugEnabled ) x.debug($msg) """ } def DEBUG2(c : Context)( msg: c. Tree, exception: c. Tree) : c. Tree = { import c