Draco-lang / Language-suggestions

Collecting ideas for a new .NET language that could replace C#
75 stars 5 forks source link

val and var #12

Closed WhiteBlackGoose closed 2 years ago

WhiteBlackGoose commented 2 years ago

About val

val allows to make a readonly field/variable/property. Similar to what Kotlin has. It does not guarantee the structure being readonly, but the value itself is. Syntax:

val a = ...

About var

var is identical to C#'s var. It is normally highlighted by the editor.

Declaration:

var a = ...

Explicit type

Define it after :

var a : string = ...
val a : string = ...

Unresolved

Should we make reassignment more explicit, like this:

a <- a + 1

or more "usual", like this:

a += 1
LPeter1997 commented 2 years ago

var and val

Mutable and immutable variable declarations should definitely be differentiated.

If we agree on issue #7, we should definitely take some inspiration from Rust in this case. Having var and val makes it way too easy to just get var into muscle memory and use it all the time. Having an explicit mutability modifier could really benefit here (mut var?).

Reassignment

As much as I don't like to rely on heritage behavior, I think the classic =, += and friends are well-established enough to keep them. Things like <- are really annoying to type out on some keyboard layouts and providing sensible compound versions would be harder.

WhiteBlackGoose commented 2 years ago

Having var and val makes it way too easy to just get var into muscle memory and use it all the time.

This is not the impression I got workign in Kotlin tbh. I write val without even considering otherwise unless I really need var. And the compiler and the IDE will definitely let you know if you misused var.

yamin8000 commented 2 years ago

I agree with var as variable and val as value, it's just simple and concise. Other languages let or mut just adds more confusion.