嵌套扩展方法不起作用 - Scala
Nested Extension Method does not work - Scala
我创建了一个简单的 Date DSL。我的代码是:
import java.time.{Year, LocalDate}
import Numeric.Implicits._
object Main {
implicit def wrapMonth[A:Numeric](v: A) = new {
def october = {
def of(y: Integer) = {
5
}
9
}
}
def main(args:Array[String]): Unit =
{
println(3.october of 2014)
}
}
我有错误:
value of is not a member of Int
println(3.october of 2014)
我不明白,为什么会出现这个错误,以及如何解决它。
以下将起作用,
implicit def wrapMonth[A: Numeric](v: A) = new {
def october = {
9
}
def of(y: Integer) = {
5
}
}
您不能从外部调用嵌套方法。它不可见。
编辑
implicit def wrapMonth(v: Int) = new {
def october = {
(v, 9)
}
}
implicit def wrapDay(v: (Int, Int)) = new {
def of(y: Int) = {
val c = Calendar.getInstance()
c.set(y, v._2, v._1, 0, 0)
c.getTime
}
}
我创建了一个简单的 Date DSL。我的代码是:
import java.time.{Year, LocalDate}
import Numeric.Implicits._
object Main {
implicit def wrapMonth[A:Numeric](v: A) = new {
def october = {
def of(y: Integer) = {
5
}
9
}
}
def main(args:Array[String]): Unit =
{
println(3.october of 2014)
}
}
我有错误:
value of is not a member of Int
println(3.october of 2014)
我不明白,为什么会出现这个错误,以及如何解决它。
以下将起作用,
implicit def wrapMonth[A: Numeric](v: A) = new {
def october = {
9
}
def of(y: Integer) = {
5
}
}
您不能从外部调用嵌套方法。它不可见。
编辑
implicit def wrapMonth(v: Int) = new {
def october = {
(v, 9)
}
}
implicit def wrapDay(v: (Int, Int)) = new {
def of(y: Int) = {
val c = Calendar.getInstance()
c.set(y, v._2, v._1, 0, 0)
c.getTime
}
}