Skip to content
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

Add DefFuture #557

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Add DefFuture #557

wants to merge 7 commits into from

Conversation

ronanM
Copy link

@ronanM ronanM commented Nov 7, 2024

DefFuture

In Scala, Future is not "referentially transparent" because:

  • Future is eagerly evaluated.
  • Future is memoized (the result is cached).
  1. The execution of this code:
for {
  _ <- Future(println("Start"))
  _ <- Future(println("foo"))
  _ <- Future(println("foo"))
} yield ()

will produce on the stdout: ✅

start
foo
foo
  1. The execution of this simple refactored code:
val fut = Future(println("foo"))

for {
  _ <- Future(println("Start"))
  _ <- fut
  _ <- fut
} yield ()

will produce on the stdout: ❌

foo
start

This is a "surprising" buggy behavior for Scala beginners 🤯.

To return to the situation in 1), the code must be written like this:

def fut = Future(println("foo")) // <--- Note the `def` instead of `val` here.

for {
  _ <- Future(println("Start"))
  _ <- fut
  _ <- fut
} yield ()

will produce again on the stdout: ✅

start
foo
foo

@ronanM ronanM requested a review from xuwei-k as a code owner November 7, 2024 12:58
@ronanM
Copy link
Author

ronanM commented Nov 24, 2024

@xuwei-k Is it possible to run the CI please ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant