Akuli / jou

Yet another programming language
MIT License
11 stars 4 forks source link

Const/mut pointers #395

Open Akuli opened 10 months ago

Akuli commented 10 months ago

When working with pointers, you can avoid many bugs by marking them as const or mutable. For example, string literals should never be modified, so their type in C is const char *, which means that "hello"[2] = 'x' will fail to compile. (Actually it is a mutable char * pointer for historical reasons, but ideally it would be const char *.)

Jou could do something similar, but I want the syntax to be as obvious as possible. For example, I think it would be good to require a mut or const keyword instead of making one of them the default (Rust's default is const pointer, C's default is mutable).

Some syntax ideas:

# The "obvious" thing for someone coming from other languages
string: byte const* = "Hello"
int_array: int mut* = calloc(10, sizeof int_array[0])
string_array: byte const* mut* = calloc(20, sizeof string_array[0])

# The "obvious" thing for someone new to pointers
string: const* to byte = "Hello"
int_array: mut* to int = calloc(10, sizeof int_array[0])
string_array: mut* to const* to byte = calloc(20, sizeof string_array[0])
Akuli commented 9 months ago

Another idea: Add compiler warnings for common constness mistakes, like trying to mutate a string literal, and keep the syntax as is. I like the simplicity of pointer syntax without constness, and I think it is a good choice for Jou.