Skip to content

Different result of inlining if an inline parameter is a Unit #20082

Discussion options

You must be logged in to vote

This has to do with the implementation of the beta-reduction optimization.

inline def pipe[T, U](inline t: T, inline f: T => U): U = f(t)

def test1 = pipe((), (u: Unit) => u)
def test2 = pipe(10, (x: Int) => x)

These technically get inlined as

def test1 = ((u: Unit) => u).apply(())
def test2 = ((x: Int) => x).apply(10)

but when inlining we also do a best-effort beta-reduction optimization. Semantically, these reduce to

def test1: Unit = { val u: Unit = (); u }: Unit
def test2: Unit = { val x: Int = 10; x }: Int

But then we also attempt to elide redundant bindings for constants. This removes the val x in test2. The Unit value is not exactly pure in bytecode terms as is ends up being a r…

Replies: 1 comment 10 replies

Comment options

You must be logged in to vote
10 replies
@adamw
Comment options

@nicolasstucki
Comment options

@adamw
Comment options

@nicolasstucki
Comment options

@dabrowski-adam
Comment options

Answer selected by adamw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants