我试图重新绑定一些功能scala.math
采取并返回Float
对象,而不是Double
对象,以便我可以将它们绑定到一个函数注册商是与接受和返回Float
对象功能的工作原理。我试过这个规则:Scala函数对象之间的隐式转换?
implicit def doubleFunc2floatFunc[T <: { def apply(x:Double):Double }] (func:T) = func(_:Float).floatValue
它不起作用。编译器抱怨我的功能是(Double)=>Float
而不是(Float)=>Float
。任何人都可以将我指向正确的方向吗?
编辑:我使用这个代码如下:
package org.nathanmoos.magnificalc.exprtreelib.functions
import org.nathanmoos.magnificalc.exprtreelib.Functions
import scala.math._
object InternalFunctions
{
implicit def float2double(x:Float) = x.doubleValue
// need an implicit def doubleFunc2floatFunc
implicit def double2float(x:Double) = x.floatValue
def csc(x:Float):Float = 1f/sin(x)
def sec(x:Float):Float = 1f/cos(x)
def cot(x:Float):Float = 1f/tan(x)
def registerAll() = {
Functions.register("ln", log _)
Functions.register("log", log10 _)
Functions.register("sqrt", sqrt _)
Functions.register("sin", sin _)
Functions.register("cos", cos _)
Functions.register("tan", tan _)
Functions.register("csc", csc _)
Functions.register("sec", sec _)
Functions.register("cot", cot _)
Functions.register("sinh", sinh _)
Functions.register("cosh", cosh _)
Functions.register("tanh", tanh _)
Functions.register("acos", acos _)
Functions.register("asin", asin _)
Functions.register("atan", atan _)
}
}
Functions.register
需要String
为函数的名称和功能对象将其与关联。
请显示您是如何使用代码的。 – 2011-04-23 04:54:53
@Daniel C. Sobral:完成。 – 2011-04-23 20:07:40
“Functions.register”的类型签名是什么?我认为这个错误发生在对它的调用上,对吧? – 2011-04-23 23:41:30