tj / luna

luna programming language - a small, elegant VM implemented in C
2.46k stars 148 forks source link

Variable binding and assignment #76

Open glhrmfrts opened 8 years ago

glhrmfrts commented 8 years ago

The parser already accepts let, so I was thinking something like Rust's:

let foo: string = "bar"

With type inference the type annotation is optional.

Uninitialized variables are also allowed:

let foo: int
foo = 2 * 2
# or with type inference
let bar
bar = 2 * 2

However there's some decisions to be made:

I'm looking forward to discuss this so we can continue with Luna's development.

aisk commented 8 years ago
glhrmfrts commented 8 years ago
aisk commented 8 years ago

Put it together:

if some_condition:
  let a = "foo"
else
  let a = 42
end

# what's the type of a? If we have a static linter, it's hard to lint the rest of code with operations to a.

So I think people will not write codes like this, declare the type of variable before assign value to it is acceptable in my opinion. And function scope is a bad choice I thought now (I think most static typed languages have block scope as I can remember).

And certainly the variables must be mutable above. Or we should write codes like this:

let a = if some_condition
  return "foo"
else
  return "bar"
end

This will hard to write code if we have a complex condition and multiple variable to assign, so I agree with you (mutable variable and immutable value like clojure dose).

glhrmfrts commented 8 years ago

I understand you now, it wouldn't be possible to analyze the program statically with uninitialized variables without a type or function scope.

So we have:

I think that's pretty good. I will leave this open for now though, we got a lot of work haha.

aisk commented 8 years ago

Hi can you open some issues about what to do, so people can join this repo quickly, thank you 😊

I saw the issues about what to do, some were to generic ...

glhrmfrts commented 8 years ago

I've added the most urgent thing right now (#77), when I have some more time I will add the rest.

aisk commented 8 years ago

:+1: