You can, apparently, recreate if expressions in Kotlin

As we all know, Kotlin allows extension infix functions. Kotlin also allows lambdas to look more syntax-like by being able to use them in a method call at the end of it. With both of those features, you can totally recreate the if-elseif…-else ladder expressions in Kotlin with the following methods:

typealias IfResult = Pair<Boolean, Any>

inline fun If(boolean: Boolean, block: () -> Any) : IfResult{
    return boolean to if(boolean) block() else Unit
}

inline infix fun IfResult.Else(block: () -> Any) : Any {
    return if(first) second else block()
}

infix fun IfResult.Else(result: IfResult) : IfResult {
    return if(first) this else result
}

I have no idea why I created this, and now I hate myself for doing it. Please don’t use this in production (unless you wanna get fired), and, I guess, enjoy!!

You will be surprised, but it is a well-known fact. As far as I remember, the only expression which could not be represented by function is when.

3 Likes

Well, I mean syntax-wise it is impossible to create because of stuff like is, in and -> unless they give us the ability to declare unary infix functions maybe? But then we would still need the ability to define custom operators for the -> part, and we would also need to have single-line lambdas. Additionally, we would also need to define the semicolon (or some other thing) as an operator so that it can combine the different results from the -> operators into one big result that gets returned by that lambda. Other than that, it seems quite doable with a bunch of unary infix functions, a custom operator that takes in Any and a lambda that returns Any (or the type T that the previous thing is requesting) and puts them in a pair and then some logic that takes out all the separate results of the -> operator and for each one it checks if the first component in that pair is any of the known unary function results, and if it is, then just perform the wanted functionality (e.g.: check if the parameter is of a certain type), but if it isn’t, then treat that value as being equal to the first parameter and then checks if whatever condition that was is met or not, and if it is it runs the lambda (which is the second component in the pair (or pair-like object) returned by the -> operator) and skips the rest of the checks

Now this is obviously currently not-doable, redundant, inefficient performance-wise, and ugly (because it would need to use semi-colons at the end of each line)

P.S. I am totally sleep-deprived rn so most of what I just wrote might make no sense whatsoever.