Gregofi / camel

Custom dynamic programming language
MIT License
6 stars 1 forks source link

Global variables are always mutable #13

Open Gregofi opened 2 years ago

Gregofi commented 2 years ago

The val definition does not apply to globals, it behaves the same way as var. The variable should however be immutable.

vlasakm commented 2 years ago

What is your plan for modules or REPL?

The easiest thing for the moment would be to have no globals - all variables would be lexically scoped and a single mechanism (which is also more efficient) could be used for everything. Builtin stuff could be predefined also as locals (for now). Then when modules are added in some capacity, builtins would be an implicit import or something like that.

Also what we are callign immutability should really be cannot be assigned, because the variable bindings cannot change, while the values themselves are mutable:

val array = [1, 2, 3];
array[0] = 0; // OK, the value bound to "array" is mutable
array = 2; // NOT OK, "array" cannot be assigned

AFAICT this kind of const / cannot be assigned bindings is not really common. Lua and Scheme have similar concept of global scope, but don't require declarations of global variables - any assignment to undeclared variable is an assignment to global. In Scheme all variables may be set!. Lua 5.4 has <const> local variables. Even Python allows things like import math; math.pi = 3.

Gregofi commented 2 years ago

Good question, since I have no plans 😄

I haven't given any though to modules yet. They are very far away on the non existing road map 😄 Same thing with REPL, although I don't really care for it contrast to modules, which would be nice to have.

Yes, you are right, that is what I meant.

I think javascript has this, and since it is most used language, it is kind of common. The implicit declaration is a thing I really don't like, since it promotes programming errors. I don't have much experience with Lua, but AFAIK it lets you do almost anything with the value being nil, similar to javascript undefined. I would rather the interpreter crashes if someone uses undeclared variable. Hovewer, since I know almost nothing about how are modules implemented, I'm not sure if this would complicate them later on.

vlasakm commented 2 years ago

I think javascript has this, and since it is most used language, it is kind of common.

Yes I forgot, indeed Javascript does exactly how I think you want.

So this is error:

def f() = a;

f(); // error: Access to undefined variable 'a'

val a = 2;

But, this is not:

def f() = a;

val a = 2;

f();

The implicit declaration is a thing I really don't like, since it promotes programming errors.

Agreed. As they say: "the top level (global scope) is hopeless".

Gregofi commented 2 years ago

Yes, exactly.

I have to think about it some more. I really want to have constants be constant (contrary to python pi 😄). But because globals are slower it just feels bad. I'm thinking about something similar to constexpr from C++, that you can declare constants only if they can be resolved at compile time (and so scoped lexically) with another special keyword. But it kinda complicates the language.

I would probably let this one sit for a bit.

Gregofi commented 2 years ago

Thinking about it, with the actual quick baked implementation of functions you can probably do something like this

def foo() = 1;
foo = 2;

yikes.