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

Support for logical not ! operator in binding to boolean values #7630

Closed
thisispete opened this issue Jun 23, 2022 · 5 comments
Closed

Support for logical not ! operator in binding to boolean values #7630

thisispete opened this issue Jun 23, 2022 · 5 comments

Comments

@thisispete
Copy link

Describe the problem

I'm trying to bind to a boolean value from a store, but I actually want the inverse boolean value to be bound
ie:

<Toggle bind:checked={!$unChecked}>Label</Toggle>

I get the error Can only bind to an identifier (e.g. 'foo') ...

Describe the proposed solution

would be cool to support the ! syntax

<Toggle bind:checked={$isChecked}>Label</Toggle>
<Toggle bind:checked={!$unChecked}>Label</Toggle>

Alternatives considered

current workaround:

$: isChecked = !$unChecked
$: unChecked.set(!isChecked)

<Toggle bind:checked={isChecked}>Label</Toggle>

Importance

nice to have

@Prinzhorn
Copy link
Contributor

This is a very specific special case of #3937 and #7265 so you could do sth. like bind:checked|negated

@bluwy
Copy link
Member

bluwy commented Jun 24, 2022

I'd second the issues linked by @Prinzhorn too. Svelte only supports binding to variables, not expressions.

@samuelstroschein
Copy link
Contributor

samuelstroschein commented Jul 15, 2022

I suggest not using the ! operator anyways. First, using an equality check is more readable, and second, the bang operator (if I recall correctly) can have unexpected outcomes.

Hence your example with an equality check becomes:

<Toggle bind:checked={$isChecked}>Label</Toggle>
<Toggle bind:checked={$isChecked === false}>Label</Toggle>

@tanhauhau
Copy link
Member

Since this issue is about supporting binding with !variable, eg:

<Toggle bind:checked={!$unChecked}>Label</Toggle>

and this is not going to happen. So I am going to close this issue.

What is more likely to happen would be

<Toggle bind:checked|negated={$unChecked}>Label</Toggle>

as @Prinzhorn mentioned. So if you are interested on that, do check out the issue #3937 and #7265.

@francoisschwarzentruber
Copy link

francoisschwarzentruber commented Sep 9, 2023

I agree with @Prinzhorn that it could be done with an input modifier |negated. However, using ! is super natural and would fit the spirit of the elegance of Svelte. Contrary to other modifiers like trim, note that the negation operator ! is bijective. If from !b we can get the value of b, while from s.trim() you can not get the value of s.

On the logical point of view, after bind:checked you could in principle have any literal see https://en.wikipedia.org/wiki/Literal_(mathematical_logic). The current version of Svelte supports positive literal, i.e. a boolean variable. But I do not see a reason why negative literals are not supported.

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

7 participants