我試圖重新綁定一些功能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