Skip to content

Conversation

@tertsdiepraam
Copy link
Contributor

@tertsdiepraam tertsdiepraam commented Jun 2, 2025

For now, this can only assign to local variables and not to full paths, which is what I ultimately want to achieve as a (partial) solution to #156 and other issues.

Closes #159

Having an assignment operator is also useful for things like setting values based on if-else:

let x = 0;
if something {
    x = 3;
} else {
    x = 5;
}

You can write this as:

let x = if something { 3 } else { 5 };

But that gets annoying with multiple variables or when the branches call other functions.

Finally, this will make it useful to implement loops. Without assignment, it's quite hard to make loops useful.


The other use of this is with overriding default values:

type Config {
    do_foo: bool,
    do_bar: bool,
}

function default_config() -> Config {
    Config {
        do_foo: false,
        do_bar: false,
    }
}

fn setup() -> Config {
    let config = default_config();
    config.do_foo = true;
    config
}

@tertsdiepraam tertsdiepraam marked this pull request as draft June 2, 2025 10:24
@tertsdiepraam
Copy link
Contributor Author

tertsdiepraam commented Jun 2, 2025

I think there's mostly 1 case not working properly right now:

x = x

This is hard because we both need to copy and drop x. The rest of the tests seem to pass valgrind.

I'm now pondering what the right solution is. The most proper thing to do would be to have some lifetime analysis which recognizes that the old x won't be used anymore and can therefore be moved instead of copied.

@tertsdiepraam tertsdiepraam mentioned this pull request Jun 3, 2025
@tertsdiepraam tertsdiepraam marked this pull request as ready for review June 3, 2025 18:58
@tertsdiepraam
Copy link
Contributor Author

I think that's all for this feature? It feels like such a big deal that it's hard to believe, but looks fine so far, even under Valgrind.

@tertsdiepraam tertsdiepraam changed the title Implement first version of the assignment operator Add assignment operator Jun 3, 2025
Copy link

@bal-e bal-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No logic issues, just asking to move some control flow around.

@tertsdiepraam tertsdiepraam merged commit 5a375ee into main Jun 9, 2025
13 checks passed
@tertsdiepraam tertsdiepraam deleted the assignment-mvp branch July 14, 2025 20:00
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.

Mysterious SIGILL

2 participants