In JavaScript, this is how I check if a function parameter is a function:
function foo ( p ) {
if ( typeof p === 'function' ) {
p();
}
// ....
}
How can I do the same in Groovy?
Groovy makes closures a first-class citizen. Each closure extends abstract class groovy.lang.Closure<V> and in case of undefined argument type you can use instanceof to check if parameter that was passed to a method is a closure. Something like that:
def closure = {
println "Hello!"
}
def foo(p) {
if (p instanceof Closure) {
p()
}
}
foo(closure)
Running this script generates output:
Hello!
Using concrete parameter type
Groovy allows you (and it's worth doing actually) to define a type of a method parameter. Instead of checking if p was a closure, you can require that caller passes a closure. Consider following example:
def closure = {
println "Hello!"
}
def foo2(Closure cl) {
cl()
}
foo2(closure)
foo2("I'm not a closure")
First call will do what closure does (prints "Hello!"), but second call will throw an exception:
Hello!
Caught: groovy.lang.MissingMethodException: No signature of method: test.foo2() is applicable for argument types: (java.lang.String) values: [I'm not a closure]
Possible solutions: foo2(groovy.lang.Closure), foo(java.lang.Object), find(), find(groovy.lang.Closure), wait(), run()
groovy.lang.MissingMethodException: No signature of method: test.foo2() is applicable for argument types: (java.lang.String) values: [I'm not a closure]
Possible solutions: foo2(groovy.lang.Closure), foo(java.lang.Object), find(), find(groovy.lang.Closure), wait(), run()
at test.run(test.groovy:18)
It's always a good practice to make your code type-safe, so you don't have to worry if a value passed as a parameter is a type you expect.
In Groovy, you'd want to check if p is an instanceof a Closure
def foo(p) {
if (p instanceof Closure) {
p()
} else {
println "p is $p and not a Closure"
}
}
foo { -> println "I'm a closure" }
foo('tim')
Outputs:
I'm a closure
p is tim and not a Closure
def eval = {o->([o].grep(Closure).find()?:{o})()}
println eval('foo')
println eval({->'bar'})
println eval{'baz'}
Related
I'm writing a function in Kotlin to retrieve items of type T from a database.
The user can specify an action to be invoked if no matching results are found, otherwise an IllegalArgumentException is thrown:
fun get(
...,
onNoneFound: () -> T = throw IllegalStateException("No matching results found")
): T {
...
return when (results.size) -> {
0 -> onNoneFound.invoke()
1 -> ...
else -> chooseResult(...)
}
}
The issue I'm having is that whenever the function is invoked, it seems that the IllegalStateException is thrown before the function body is executed.
In a way, that makes sense, and I suppose a workaround could be:
fun get(
...,
onNoneFound: (() -> T)? = null
): T {
...
return when (results.size) -> {
0 -> if (onNoneFound == null) {
throw IllegalArgumentException("No matching results found")
} else {
onNoneFound.invoke()
}
1 -> ...
else -> chooseResult(...)
}
}
I was wondering if there is a more elegant/preferable solution to this problem - ideally where I do not have to make the function nullable and do a null check later. Is there a way of doing this with Kotlin's default argument syntax?
Edit:
It occurred to me that I can use the elvis operator for the null check which does make the code more elegant:
onNoneFound?.invoke() ?: throw IllegalArgumentException("No matching results found")
But I'm still curious if Kotlin has a built-in way of doing this without the null check.
You shouldn't build the exception directly. Try:
fun get(
...,
onNoneFound: () -> T = { throw IllegalStateException("No matching results found") }
): T {
...
return when (results.size) -> {
0 -> onNoneFound.invoke()
1 -> ...
else -> chooseResult(...)
}
}
The problem is that throw IllegalStateException(...) is a perfectly fine expression of type () -> T, just as it is an expression of any other type; but it throws the exception immediately, what you want is a lambda which will throw the exception when invoked. And that's { throw IllegalStateException(...) } in Kotlin, as
Roger Lindsjö's answer says.
Someone know how can I define an interface for a class method like:
wrap(fn) {
return function(){
... // do something
fn()
}
}
I'm crashing my head around this, basically how can I define the type of a parameter (and a return value) to be function?
I'm supposing you wanted to return another function that have identical type of fn.
class Wrapper {
// generic for any fn, without handling parameter type
// a return type is not required: tsc infers from `return` statement.
wrap<T extends Function>(fn: T) {
return function () {
// NOTE this version does not handle types of parameters.
// you will have to use `arguments`
return fn();
} as any as T;
}
// generic for all unary fn
// we can have correct type of arg1 in this way
wrapF1<P1, R>(fn: (arg1: P1) => R) {
const wrapped = function (arg1: P1) {
return fn(arg1);
}
return wrapped;
}
// wrapF2, wrapF3, if you need more
}
Is it possible, in Scala, to define a function that would receive any other function as a parameter?
It should be something like the following:
object Module extends SecureModule{
val bc = new MyBC()
def method(parameter: Type) = {
exec(bc.method(parameter))
}
def method2(parameter1: Type1, parameter2: Type2) = {
exec(bc.method2(parameter1,parameter2))
}
}
trait SecureModule {
def exec(f: ANY_PARAMETER => ANY_RESULT) = {
//some extra processing
f
}
}
is it possible? If so, how could I achieve this?
Thank you in advance.
The nice thing about scala is that you can create what seems to be your own syntax.
If what you want to do is wrap an operation so that you can do pre and post processing, as well as control the execution context, then you do this by using call-by-name parameters. For example, if we just wanted to time how long a block of code takes, then we could do something like this:
def timer[T](block: => T): (T,Long) = {
val startDate = new Date()
val result = block
val endDate = new Date()
(result, endDate.getTime()-startDate.getTime())
}
We can use it like this:
val (result,duration) = timer {
1+3
}
Or like this
val (result,duration) = timer {
"hello" + " world!"
}
And the result will have the correct type from the block that you pass in while also giving you the duration that you expect.
I am under the impression that your description is somewhat misleading.
The way I understand it, what you (might) want to do is delaying the execution of the bc.method calls until some other code has been performed.
If so, try this:
object Module extends SecureModule{
val bc = new MyBC()
def method(parameter: Type) = {
exec(() => bc.method(parameter))
}
def method2(parameter1: Type1, parameter2: Type2) = {
exec(() => bc.method2(parameter1,parameter2))
}
}
trait SecureModule {
def exec[Result](f: () => Result): Result = {
//some extra processing
f()
}
}
You can't take any function as a parameter. What would you even do it?
At best, you can take any function that has a specific number of parameters.
For example, here, f takes one argument and returns a value.
def exec[A,B](f: A => B)
And here, f takes two arguments:
def exec[A,B,C](f: (A, B) => C)
If you don't care about the return type of the function, you could always use Any instead of a type parameter, since functions are covariant in their return type:
def exec[A](f: A => Any)
Is there a way to declare a function such that I don't care about return value? For example I have this Array extension:
extension Array {
func forEach(function: (element: T) -> ()) {
for e in self {
function(element: e)
}
}
}
Now I want to do:
textFields.forEach{$0.resignFirstResponder()}
And I can't because the function is declared to return Void.
This would fix it:
textFields.forEach{$0.resignFirstResponder();return}
But is there a generic way to declare the function such that I can return any value or Void?
Thanks!
If you add a second generic parameter with no constraints, and type the function to return it, then any return value would be accepted:
extension Array {
func forEach<U>(function: (Element) -> U) {
for e in self {
function(e)
}
}
}
func f(i: Int)->Int {
return i * 2
}
func g(i: Int) -> Double {
return Double(0.0)
}
func h(i: Int) {
println("\(i)")
}
let a = [1,2,3]
a.forEach(g) // U will be an Int
a.forEach(f) // U will be a Double
a.forEach(h) // U will be a ()
However, I’d strongly suggest you not do this and use for…in instead.
The only purpose of running a function on an array but not using the return value is for side-effects. When writing in a more imperative style and using side-effects and external state (as opposed to a more declarative functional style) it’s much better to use for…in as a signal that this is what you’re doing.
There are also unpleasant gotchas involved in constructing your own pseudo-control structures. For example, think about this:
// function that does something side-effecty, but
// if it achieves some goal, exist early
func someFunc(a: [Int]) {
a.forEach { i -> () in
// goal achieved early,
// return from func
return
}
assert(false)
}
This is a common practice – cut out of a function early if some goal (or failure) is achieved. But with the forEach structure this is a bit misleading. It looks like a regular loop, but it’s actually a closure, and the return returns from that closure, ready for forEach to call the next one (essentially, it behaves like a continue in a regular loop).
Use map():
let textFields : [UITextField] = [] // or whatever
textFields.map{$0.resignFirstResponder()}
This way you don't have to specify the return type, and you don't need to reinvent map with your forEach extension.
I am trying to move some code from a grails service file into src/groovy for better reuse.
import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
class JsonUtils {
// seems like a clunky way to accomplish converting a domainObject
// to its json api like object, but couldn't find anything better.
def jsonify(obj, ArrayList removeableKeys = []) {
def theJson = obj as JSON
def reParsedJson = JSON.parse(theJson.toString())
removeableKeys.each { reParsedJson.remove(it) }
return reParsedJson
}
// essentially just turns nested JSONObject.Null things into java null things
// which don't get choked on down the road so much.
def cleanJson(json) {
if (json instanceof List) {
json = json.collect(cleanJsonList)
} else if (json instanceof Map) {
json = json.collectEntries(cleanJsonMap)
}
return json
}
private def cleanJsonList = {
if (it instanceof List) {
it.collect(cleanJsonList)
} else if (it instanceof Map) {
it.collectEntries(cleanJsonMap)
} else {
(it.class == JSONObject.Null) ? null : it
}
}
private def cleanJsonMap = { key, value ->
if (value instanceof List) {
[key, value.collect(cleanJsonList)]
} else if (value instanceof Map) {
[key, value.collectEntries(cleanJsonMap)]
} else {
[key, (value.class == JSONObject.Null) ? null : value]
}
}
}
but when I try to call jsonify or cleanJson from services I get MissingMethodExceptions
example call from grails service file:
def animal = Animal.read(params.animal_id)
if (animal) json.animal = JsonUtils.jsonify(animal, ['tests','vaccinations','label'])
error:
No signature of method: static org.JsonUtils.jsonify() is applicable for argument types: (org.Animal, java.util.ArrayList) values: [ ...]]\ Possible solutions: jsonify(java.lang.Object, java.util.ArrayList), jsonify(java.lang.Object), notify()
Also tried making the jsonify take an animal jsonify(Animal obj, ...) then it just said Possible solutions: jsonify(org.Animal, ...
The cleanJson method was meant to deal with JSONObject.Null things which have caused problems for us before.
example call:
def safeJson = JsonUtils.cleanJson(json) // json is request.JSON from the controller
error:
groovy.lang.MissingMethodException: No signature of method: static org.JsonUtils.cleanJson() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONObject) values: [[...]]
Possible solutions: cleanJson(org.codehaus.groovy.grails.web.json.JSONObject)
All this code worked as it is when it was in service file. I am running grails 2.3.11 BTW
You've declared jsonify() and cleanJson() as instance methods and try to use them as static. Declare them as static and it should work:
class JsonUtils {
def static jsonify(obj, ArrayList removeableKeys = []) {
(...)
}
def static cleanJson(json) {
(...)
}
}
You need to define jsonify() and cleanJson() as static in order to call them statically.