Skip to content

Instantly share code, notes, and snippets.

@aztek
Created April 27, 2012 20:41
Show Gist options
  • Save aztek/2512901 to your computer and use it in GitHub Desktop.
Save aztek/2512901 to your computer and use it in GitHub Desktop.
Scala functions defined in JavaBeans
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="true" default-autowire="byName">
<bean id="increment" class="com.myapp.IntegerFunctions" factory-method="increment" />
<bean id="decrement" class="com.myapp.IntegerFunctions" factory-method="decrement" />
<bean id="appendBangs" class="com.myapp.IntegerFunctions" factory-method="lolNoIntegers" />
<bean id="doubleIncrement" class="com.myapp.IntegersFunctionDoubleApplication">
<property name="function" ref="increment"/>
</bean>
<bean id="doubleDecrement" class="com.myapp.IntegersFunctionDoubleApplication">
<property name="function" ref="decrement"/>
</bean>
<!-- Will pass validation but cause runtime error due to type erasure :( -->
<bean id="someShit" class="com.myapp.IntegersFunctionDoubleApplication">
<property name="function" ref="appendBangs"/>
</bean>
</beans>
package com.myapp
object IntegerFunctions {
val increment: Int => Int = _ + 1
val decrement: Int => Int = _ + 1
val lolNoIntegers: String => String = _ + "!!!"
}
package com.myapp
import reflect.BeanProperty
import org.springframework.beans.factory.annotation.Required
class IntegersFunctionDoubleApplication {
@BeanProperty
@Required
var function: Int => Int = _
def applyTwice = function andThen function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment