#!/bin/env scala | |
trait 所有操作 { | |
protected type 操作符 | |
protected def 创建操作符(操作: (Int, Int) => Int):操作符 | |
final def 加 = 创建操作符(_ + _) | |
final def 乘 = 创建操作符(_ * _) | |
} | |
implicit final class 整数(左值: Int) extends 所有操作 { | |
protected type 操作符 = Int => Int | |
protected final def 创建操作符(操作: (Int, Int) => Int) = { 右值 => 操作(左值, 右值) } | |
case class 一直(操作: (Int, Int) => Int) { | |
def 到(右值: Int) = { | |
(左值 to 右值).reduce(操作) | |
} | |
} | |
def 到(右值: Int) = 左值 to 右值 | |
} | |
object 延后计算 extends 所有操作 { | |
protected type 操作符 = (Int, Int) => Int | |
protected final def 创建操作符(操作: (Int, Int) => Int) = 操作 | |
} | |
import 延后计算._ | |
println(1 加 10) | |
println(1 乘 10) | |
println(1 到 10) | |
println(1 一直 加 到 10) | |
println(1 一直 乘 到 10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment