通常使用Scala闭包实现Java Single-Abstract-Method接口? [英] Generically implementing a Java Single-Abstract-Method interface with a Scala closure?
问题描述
据我了解,当它们最终出现时,我们将能够用Java闭包代替等效的单方法接口。是否有标准的Scala习惯用法做同样的事情 - 使用Scala闭包实现Java Single Abstract Method接口?
As I understand it, when they finally come along, we will be able to substitute a Java closure for the equivalent single-method interface. Is there a standard Scala idiom for doing the same - implementing a Java Single Abstract Method interface with a Scala closure?
理想情况下,我希望以下内容能够自动运行
Ideally I'd like the following to automagically work
test("Closure") {
var event: PropertyChangeEvent = null
var label = new JLabel()
label.addPropertyChangeListener( {e: PropertyChangeEvent => event = e} )
label.setText("fred")
event.getNewValue should be ("fred")
}
推荐答案
1月份对此进行了长时间的讨论。
There was a lengthy discussion about this in January.
http://www.scala- lang.org/node/8744
这个想法很受欢迎,甚至比这个提案更进一步。但是魔鬼在细节中,原型实施可能会在此提案中找到其他问题。
The idea was well received, and even taken a bit further than this proposal. But the devil is in the details, and a prototype implementation may yet find other problems with this proposal.
这篇关于通常使用Scala闭包实现Java Single-Abstract-Method接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!