mun icon indicating copy to clipboard operation
mun copied to clipboard

should `;`s really be optional?

Open MineBill opened this issue 5 years ago • 2 comments

I have no idea how to call this issue, so if you think there a better name please change it.

Basically, while messing around with godot and mun, i figured out that this was possible:

// This compiles successfully and returns 999
pub fn random() -> i32 {
    2 2 2 2 2 2 2 2 2 999
}

Also this:

// Returns a, which is 0
pub fn random() -> i32 {
    let a = 0 a
}

// THIS DOESN'T COMPILE
pub fn random2() -> i32 {
    let a = a 0
}

And stuff like this:

// Returns 999
pub fn random() -> i32 {
    let a = 0 0 0 0 0 0 0 
    1 2 3 4 1 2 3 4 1 2 34 1 2 
    1.0 3.1 1 a 20 a a a a 999
}

So it looks like it's ignoring everything and returns what it's supposed to but i don't think you should be able to write anything like that.

MineBill avatar Jun 17 '20 20:06 MineBill

Hey @MineBill , this is actually all perfectly valid Mun code. The reason being that ;s are optional in Mun.

pub fn random() -> i32 {
    2 2 2 2 2 2 2 2 2 999
}

is parsed as

pub fn random() -> i32 {
    2; 2; 2; 2; 2; 2; 2; 2; 2; 999
}

since the last expression is the return expression, 999 is returned.

This:

// Returns a, which is 0
pub fn random() -> i32 {
    let a = 0 a
}

// THIS DOESN'T COMPILE
pub fn random2() -> i32 {
    let a = a 0
}

is parsed as:

// Returns a, which is 0
pub fn random() -> i32 {
    let a = 0; 
    a // a is the return expression 
}

// THIS DOESN'T COMPILE
pub fn random2() -> i32 {
    let a = a; // a is not yet defined in the initializer so this fails.
    0 // 0 is the return expression
}

I get the confusion though, would you prefer if ;s are not optional? Typescript also has optional ; but a new expression must reside on a newline, maybe that makes more sense?

baszalmstra avatar Jun 17 '20 20:06 baszalmstra

I had no idea ; were optional. All of the examples used ;s and since Mun tries to look like Rust, i assumed ;s were required. I don't really have an opinion on whether ; should be optional or not but if the remain optional, i think that new expressions should reside on new lines. At the very least, ;s should be documented on the Book.

MineBill avatar Jun 17 '20 21:06 MineBill