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

Variable const error and immutable proposal. #1866

Open
EchoPouet opened this issue Jan 19, 2025 · 0 comments
Open

Variable const error and immutable proposal. #1866

EchoPouet opened this issue Jan 19, 2025 · 0 comments

Comments

@EchoPouet
Copy link

EchoPouet commented Jan 19, 2025

Hello,

I would like convert a simple Zig code to test C3 features, the code:

import std::io;

fn int main()
{
    char[8] some_primes = {1, 3, 5, 7, 11, 13, 17, 19};

    some_primes[0] = 2;

    const char FIRST = some_primes[0];

    const char FOURTH = some_primes[3];

    const usz LENGHT = some_primes.len;

    io::printfn("First: %d, Fourth: %d, Length: %d", FIRST, FOURTH, LENGHT);

    return 0;
}

And I have this error mesage:

 6: 
 7:     some_primes[0] = 2;
 8: 
 9:     const char FIRST = some_primes[0];
                           ^^^^^^^^^^^^^^
(003_arrays.c3:9:24) Error: The expression must be a constant value.

 8: 
 9:     const char FIRST = some_primes[0];
10: 
11:     const char FOURTH = some_primes[3];
                            ^^^^^^^^^^^^^^
(003_arrays.c3:11:25) Error: The expression must be a constant value.

"const" indicate that the variable can't be re-assigned and it's the case with my example code. Also I found Constant expressions and I doesn't understand why it can't work because FIRST and FOURTH take integer value. I'd agree if these variables were pointers or references, but it's not the case.

After explanation in Discord I made a proposal.

Like some others languages (Nim, Rust ...) I propose "let" statement to declare symbols are single assignment variables: After the initialization their value cannot change.

import std::io;

fn int main()
{
    char[8] some_primes = {1, 3, 5, 7, 11, 13, 17, 19};

    some_primes[0] = 2;

    let char first = some_primes[0]; // OK

    let char fourth = some_primes[3];

    first = 8; // Error, let variable can't be re-assigned

    const usz LENGHT = some_primes.len;

    io::printfn("First: %d, Fourth: %d, Length: %d", first, fourth, LENGHT);

    return 0;
}

Thanks.

@EchoPouet EchoPouet changed the title Variable const error, explain me why Variable const error and immutable proposition. Jan 20, 2025
@EchoPouet EchoPouet changed the title Variable const error and immutable proposition. Variable const error and immutable proposal. Jan 20, 2025
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

No branches or pull requests

1 participant