thebennybox-Community / Community-Compiler

MIT License
16 stars 5 forks source link

Dec Statments #27

Open Myvar opened 6 years ago

Myvar commented 6 years ago
dec a : i32;
dec a : i32 = 10;
dec a = 10i32;
dec a = "string";
dec a = true;

// When no explicit type is provided for a number, i32 will be assumed
dec a = 10;

// When no explicit type is provided for a floating point number, f32 will be
// assumed
dec a = 10.0128;
davidgarland commented 6 years ago

In my opinion dec makes no sense; it literally just means declare.

What you should do is have separate declarators for mutable and immutable types; I prefer the style that Nim, Swift, and JavaScript use:

var a: i8 = 1 # Mutable variable.
let b: i8 = 2 # Immutable variable.

Because it is used in these languages it makes more sense from an ergonomic familiarity standpoint because the average user won't be baffled by having to guess what dec and const would mean-- "const is still declaring something, so why isn't it dec?!"

Additionally this leaves const open for use for constant expressions, as Nim uses it:

proc add(a, b: int): int =
  result = a + b

const i = add(1, 1) # Evaluated at compile time. No code is generated for this.
echo i # Equivalent to "echo 2"

this is equivalent to making the function C++ constexpr, but Nim has an interpreted version of itself which it can use to run the function ahead of time, meaning explicitly constexpr functions are unnecessary.

SpencerTorres commented 6 years ago

With the desire to use this language for game development, I suggest we keep the syntax relatively simple. Many people look for good(and simple) starter languages to make games with, so using almost 20 characters to define an example integer may not be too inviting or convenient long-term. I suggest starting as simple as possible, and then adding complexity as needed.

Myvar commented 6 years ago

We will review your sugestions when we get to this part, for now we want a super baisc system just to bootsrap then we will get down in the dirst and add facny things a design proper stuff.

davidgarland commented 6 years ago

Mutability isn't "fancy"-- its a huge thing to have for optimization reasons and also semantic reasons if the programmer doesn't want something to change.

Regardless I think we should have it pre-bootstrap-- using "dec" instead of "var"/"let" ahead of time makes no sense.

At the very least replace "dec" with "var" and leave "let" out if you absolutely must.

Myvar commented 6 years ago

maby its a little bit more concistant to add a immut key word

so like

let immut x = 10;

??

davidgarland commented 6 years ago

That isn't any more concise-- and it's actually the opposite of what Rust does, and encourages mutability.

In Rust let is used for both, but mut is the qualifier for mutation-- which is a purposeful thing that the Rust devs did to encourage the use of immutable variables, since typing it would be shorter. Encouraging immutability is good practice as it is far easier to optimize since the compiler doesn't have to guess if the value has changed if it is a reference, and also can save the compiler from making local copies of a variable for functions in some cases where optimization isn't the best-- which will be us early on.

However, I think var/let strikes a nice balance between the two ideas-- let sounds like you're making a mathematical declaration, and in math you don't re-assign a value to a variable-- you tack the "prime" symbol onto it and make a new one. Meanwhile "var" is obviously a shortening for "variable" and the meaning behind it is obvious.

Platin21 commented 6 years ago

I agree with @davidgarland var / let do simply look better and provide a more clear picture. Probably a new issue for compile time evaluation as how much should we allow or should we even restrict languages like odin or jai also don't restrict users.

Myvar commented 6 years ago

ok, i have spoken to the rest, and we will use let and var, like you guys sugested, we are implmenting it atm