https://kotlinlang.org logo
#random
Title
# random
y

Youssef Shoaib [MOD]

02/25/2024, 9:08 PM
Anyone know how to achieve kotlin-like
inline
behaviour in Scala? Does something like this just work?
Copy code
inline def forEach(inline block: Int => Unit): Unit
With
block
getting mentioned only once inside
y

Youssef Shoaib [MOD]

02/26/2024, 1:27 PM
I mean the other way around. I want to write efficient
map
s for instance in Scala, or a for-each, but without a closure actually being creates
e

ephemient

02/26/2024, 1:27 PM
assert
is hard-wired in the compiler to be lazy in its argument but otherwise there isn't
why though?
oh wait you're asking a different question than I thought
1
y

Youssef Shoaib [MOD]

02/26/2024, 1:28 PM
Mostly as an academic exercise. I want to use such code while still being able to argue that it's efficient enough.
inline
gives me that in Kotlin, so I'm hoping that there's an equivalent in Scala, and figured if anyone would know it would be people here
y

Youssef Shoaib [MOD]

02/26/2024, 1:33 PM
Yeah so would the following code do the trick, or do I need some complicated
Quotes
stuff?
Copy code
inline def forEach(inline block: Int => Unit): Unit
Also I'm guessing there's no nice (and performant) way to do non-local returns, so I would likely need to have
block
return whether to keep iterating or not
e

ephemient

02/26/2024, 1:35 PM
yeah I'm not sure from their docs
k

Klitos Kyriacou

02/26/2024, 3:56 PM
assert
is hard-wired in the compiler to be lazy in its argument
That's only in Java, not in Kotlin.
k

Klitos Kyriacou

02/26/2024, 4:07 PM
That's strange, I'm using 1.9.22 and the following prints "AB":
Copy code
fun main(args: Array<String>) {
    assert(false.also { print("A") } )
    println("B")
}
e

ephemient

02/26/2024, 4:08 PM
ah,
-Xassertions=jvm
isn't default, but it is the default (only) behavior on native and js
thank you color 1