Skip to content

doc(inline): improve recursive example #8235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 9, 2020
Merged

Conversation

robstoll
Copy link
Contributor

@robstoll robstoll commented Feb 7, 2020

Either my assumption about by-name parameters in the context of inline functions is wrong, i.e. they are not lazily evaluated or the example was wrong.
I have changed the example. Its more complicated but hopefully the difference between inline and by-name is more obvious this way.

I have put inline def on a new line as there was no space, it looks as follows on the website inline def.inline

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Feb 7, 2020

By-name arguments are evaluated on each reference

def f(a: =>Unit): Unit = {
  a
  a
  a
}
f { println("hello") }

prints

hello
hello
hello

@robstoll
Copy link
Contributor Author

robstoll commented Feb 8, 2020

Oh man 🤕 seems like I always made a wrong assumption about by-name parameters. Although https://docs.scala-lang.org/tour/by-name-parameters.html states it quite clearly.
I have taken your example but put an emphasise on the fact that inline just inlines and thus can make the code bigger (but on the other hand allows for constant folding)

@nicolasstucki
Copy link
Contributor

Don't worry about it. Most use cases of by-name parameters use it 0 or 1 times, in which case it is semantically equivalent to lazy val but more performant.

@nicolasstucki nicolasstucki merged commit 68ed7a3 into scala:master Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants