want to have something like fun funGenerator(som...
# functional
n
want to have something like fun funGenerator(some flags) { if flag = a return fn(param) { some operation on param } if flag = b return fn(param1 param2} {some different operations } }
g
first of all define return type, as I mentioned in another thread, also, usually easier to use lambda instead of anonymous function
hah, funny, actually, for your case anonymous function is just fine, because do not clash with
when
block:
Copy code
fun funGenerator(flag: Int): () -> Unit {
        return when (flag) {
            1 -> fun() { /* Do something for flag 1 */ }
            2 -> fun() { /* Do something for flag 1 */ }
            else -> error("Unknown flag")
        }
    }
n
That's exactly the kind of stuff I'm trying to move out of a different function. Thanks, I can work with this
Thought Unit was just for returning void
g
Kotlin doesn’ have
void
, so yeah, use Unit or any type which this function should return
you also can use lambdas instead, anonymous function is very rare thing on my practice
p
are local functions expressions?
or statements
g
Expressions. Local anonymous function is almost the same as lambda, the biggest difference is that you can use return inside of anonymous function
p
yeah they get compiled as lambdas, I didn’t know you could also assign them to a variable. We JS, guise.
n
ended up rewriting them as lambda's. lot cleaner