JaiPrimer
JaiPrimer copied to clipboard
Question about variable affectation
Hi,
As I new comer here is my first reaction to the syntax:
counter: int = 0;
Oki fair enough. It seems that a colon is used to define new variables... Oki I think I get it.
counter := 0;
Wait a minute, now the colon seems to be part of ":=" pair... What does this mean. It seem colon as a contextual meaning. Is counter: = 0; equivalent? This is confusing.
counter: int;
Yet another syntax to remember.
Do we really need a colon here? Is it to express a new variable declaration or assign a type explicitly? I think the use of it in this first example is confusing... Can't we just remove it entirely?
Just saying that having a confusing syntax as a first example is off putting.
The :=
is just to represent that the type is inferred. Like a: auto = b;
.
a: int
and a: int = 0
are the same, but the latter initializes it with a default value. Like int a
vs int a = 0
.
What Ryan (kirbyfan64) said is true. Here's another attempt at explanation.
The name of the variable always comes first. The colon denotes the type of the variable, and the = denotes an assignment to a value. So, name : type = value;
. Sometimes the type can be inferred from the assignment, and in those cases the type is omitted and it looks like name := value;
. Like Ryan said, that would be the same as name : auto = value;
If you don't want to assign a particular value, you can omit the assignment: name : type;
. I think Jai will still initialize it to 0 in that case though, and you have to do something like name : type = ---;
to avoid initialization at all.
The current readme doesn't sell ':=' for me at all.
It seems to map essentially 1:1 with the existing C++ approach.
a := b
-> 'auto a = b
a : t->
t a`
...so what are the advantages?
I like the explicit uninitialized value... but that could be a simple extension to the C++ approach.
It seems to me that this is trying to mimic a more functional style... but that is weird for a language that's supposedly C-style.