You are currently viewing Are the expected/actual results of this simple function expected?

Are the expected/actual results of this simple function expected?

I have this function, where I’ve commented the expected v actual results of invoking it:

fun multiply(test: Int): Int { return if (test == 1) { 1 // returns 1, expect 10 } else if (test == 2) { 2 // returns 2, expect 20 } else if (test == 3) { 3 // returns 30, expect 30 } else { 4 // returns 40, expect 40 }.times(10) } 

I can see from the decompiled Java code why this is happening:

public static final int multiply(int test) { return test == 1 ? 1 : (test == 2 ? 2 : (test == 3 ? 3 : 4) * 10); } 

But it seems confusing and not at all what I expect. If I wrap the entire `if` with `()` it works as expected. Just wondering if this is an intentional design decision and if so, why?

submitted by /u/Stickerbox
[link] [comments]