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

Effect.if evaluates onTrue when condition is false #2345

Closed
alex-dixon opened this issue Mar 16, 2024 · 3 comments
Closed

Effect.if evaluates onTrue when condition is false #2345

alex-dixon opened this issue Mar 16, 2024 · 3 comments

Comments

@alex-dixon
Copy link
Contributor

alex-dixon commented Mar 16, 2024

What version of Effect is running?

2.2.3

What steps can reproduce the bug?

import { Effect } from 'effect'

const f = (s: string) => Effect.succeed(s)

const foo: { bar?: { baz: string } } = {}

Effect.runSync(
  Effect.if('bar' in foo, {
    onTrue: f(foo.bar!.baz),
    onFalse: Effect.fail('oops'),
  })
)

What is the expected behavior?

onTrue does not evaluate when truth test is false

What do you see instead?

/repro/scratch.ts:9
    onTrue: f(foo.bar!.baz),
                       ^
TypeError: Cannot read properties of undefined (reading 'baz')
    at Object.<anonymous> (/repro/scratch.ts:9:24)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module.m._compile (/repro/node_modules/ts-node/src/index.ts:1618:23)

Additional information

No response

@alex-dixon alex-dixon added the bug Something isn't working label Mar 16, 2024
@tim-smart
Copy link
Member

You will need to wrap it with Effect.suspend

@mikearnaldi mikearnaldi added working as intended and removed bug Something isn't working labels Mar 20, 2024
@mikearnaldi
Copy link
Member

You will need to wrap it with Effect.suspend

might be worth considering if conditional branches should be lazy, while the fact that the branch would evaluate is pretty obvious from JS eager evaluation semantics the golden path should be not to evaluate the branch

@tim-smart
Copy link
Member

Closed by #2451

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

No branches or pull requests

3 participants