English 中文(简体)
情景:间接转换为产生方法价值?
原标题:Scala: implicit conversion to generate method values?

如果我在Schala有以下班子:

class Simple {
  def doit(a: String): Int = 42
}

以及这一类的事例

o = new Simple()

是否有可能界定一种将这种情形和一种在编纂成像这样的图象时知道的方法的暗中转换?

Tuple2 (o, (_: Simple).doit _)

我希望我能够本着以下精神来登记职能呼吁:

doThisLater (o ->  doit)

我在职能上拥有根据对的复方功能,但添加这一厚度的合成糖是巨大的。

最佳回答

你们只能把这种方法推到别处。 Sample REPL session,

scala> case class Deferred[T, R](f : T => R)
defined class Deferred

scala> def doThisLater[T, R](f : T => R) = Deferred(f)
doThisLater: [T, R](f: T => R)Deferred[T,R]

scala> val deferred = doThisLater(o.doit _)  // eta-convert doit
deferred: Deferred[String,Int] = Deferred(<function1>)

scala> deferred.f("foo")
res0: Int = 42
问题回答

暂无回答




相关问题
c# reflection with dynamic class

I need to execute a method "FindAll" in my page. This method returns a list of the object. This is my method that I execute "FindAll". FindAll requires an int and returns an List of these class. ...

Performance overhead of using attributes in .NET

1.. Is there any performance overhead caused by the usage of attributes? Think for a class like: public class MyClass { int Count {get;set;} } where it has 10 attibutes (...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

Testing private method of an abstract class using Reflection

How can I test a private method of an abstract class using reflection (using C#)? I am specifically interested in adapting the code found in this thread. I am aware of the discussion around the ...

Adding Items to ListBox, RadioList, Combobox using reflection

I m trying to add items to a listbox,combobox, radiolist using reflection. The code I have at the moment is as follows: public static Control ConfigureControl(Control control, ControlConfig ctrlconf)...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签