deathbeam / spoon

:ramen: Spoon is a programming language that runs blazingly fast, compiles to native code and works everywhere.
https://spoonlang.org
MIT License
56 stars 11 forks source link

Declaration syntax #23

Closed deathbeam closed 8 years ago

deathbeam commented 8 years ago

So I am opening this issue, because this is open-source project and I am trying to make it more "community-driven". So, I am deciding about new syntax for defining a variable.

Currently, we have this syntax=

# We define something here
def something = 8

def somethingElse : String = "Hello"

# Now we will change its value
something = 9

And I want to remove the def and add new := keyword:

# We define something here
something := 8

# We do not need to use := here, because we used the `:` when specifying the type
somethingElse : String = "Hello"

# Now we will change its value
something = 9

So?

fponticelli commented 8 years ago

What if at some point in the future you want to add immutable variables, you define a new operator?

fponticelli commented 8 years ago

also what if you want to define but not assign? the classic example would be:

def something;
if(condition)
  something = "test"
deathbeam commented 8 years ago

def something isnt valid syntax anyway. You need to atleast use def something : String. But yes, I also though about that. If you will want to define something and not assign, you will use default value for your type, so false for booleans, 0 for numbers and null for rest.

Tunmix commented 8 years ago

:= FTW !

kobi2187 commented 8 years ago

I like it! 'def' is used for functions, and 'var' for variables, and then this suggestion is better. when inside a function, you can assign directly. That's very nice. for your information, there is another language that does that: http://cobra-language.com/ if you want to check pitfalls of your idea doesn't seem to have a problem.

kobi2187 commented 8 years ago

What about returning or assigning multiple values?

a,b := 3,4 or
a,b := return2() or a,b,rest = [1,2,3,4,5] # a = 1, b = 2, c = [3,4,5]

when you add parsing to this feature, think if you want to support multiple assignment.

escargotprodige commented 8 years ago

:+1:

osakared commented 8 years ago

Will removing the def mean that you can declare the class member inside your ctor (a la ruby and python)? Because I don't like how you currently have to repeat yourself like in C-like languages currently.

osakared commented 8 years ago

Would it be bad to treat the first assignment as a declaration as well and default to Dynamic for the type (or simply leave it out and let haxe sort out if it's allowable or not)

deathbeam commented 8 years ago

First assignment as declaration can be very hard with current compiler state. And what do you mean declaring the class member inside "actor"?

osakared commented 8 years ago

ctor = consctuctor. Like I like how in ruby and python, you can just do this:

class Blah:

    # I don't need to declare count here

    def __init__(self):
        self.count = 0

It helps prevent repeating yourself to only have to declare it one place or another. Also reduces the likelihood of having an uninitialized member variable.

kobi2187 commented 8 years ago

that's like Lua. There is a problem with this approach: what if you have a spelling mistake? the compiler will allow this. a static language is much safer in this regard. my opinion is that it doesn't fit raxe, but that's only one opinion among many.

deathbeam commented 8 years ago

I am going to use the Ruby-Python way of declaring variables, so closing this.