Rust with indents (Python, CoffeeScript, Haskell, F# style)


#21

I think this is a bad idea.

  1. Structs still need braces, having braces everywhere is more consistent.
  2. Braced expressions are easy to parse visually. I find Rust’s braces interact really well with the expression-oriented features of the language (if expressions and matches).
let x = match y {
    Something => {
        // do some stuff with  y
        some_function(y)
    },
    _ => ()
};

VS

let x = match y
    Something => scope
        // do stuff with y
        some_function(y)
    _ => ()

I think the braced option looks less fragile, as your eyes can group each pair of braces into neat little expressions.


#22

I’ve actually always found indents to be something that helps my visual grouping more than braces, since being able to pair braces and visually recognize subscopes sort of depends on those braces following familiar indenting rules anyway.

What about using a Pythonic-style colon (or some other character) to indicate only the beginning of a subscope?

pub fn foo(x: uint):

if x > 1:

println!("x > 1");

:

let x = 1i;

println!("x == 1 now");

println!(“now x is {}”, x);


#23

Thumbs down from me.

I don’t like meaningful whitespace in Python, but I don’t mind it in Haskell, and I think that’s because Haskell is inherently more declarative. Rust is like Python in that it is more procedural than declarative, plus I do think it’s important to keep scope as explicit as possible.


#24

Ruby has do blocks for that, so I guess there’s precedent for having both blocks semantics and meaningful whitespace.


#25

Strong -1 on this one. It’s not making the code any cleaner and it will cause loads of problems for APIs and macros. Instead we should find more language features to avoid unnecessary nesting. try! and friends are a good way to keep code shallow.


#26

+1 for the idea -2 if it becomes part of rust

I really like Python and meaningful indents but having a option enabling this in the compile is from my point of view a very bad idea:

  1. Just adding indents is not possible to make it wok you need to do more changes like changing the if statement (e.g. adding a trailing : ) having some block indention syntax etc.
  2. There is a risk to end up like CoffeScript (and somehow Scala) with a syntax witch can produce highly ambiguous lines
  3. The complexity of the compiler will increase
  4. There might be problems with mixing normal and indent syntax
  5. There will be a big problem with macros. Either you need a complete different macro syntax for both styles or limit macros to use the normal syntax
  6. etc (I had at last one point more but forgot)

Due to the aboves limitations adding indention to rustc is probably a bad idea.

Nerveless I think creating a additional precompiler in a extern project is a nice idea (in a similar fashion as CoffeScript->Js) also adding support for reading non .rs files through a precompiler to rustc would be intresting but maybe misleading.


#27

Have you ever read any CoffeeScript that someone else wrote? To me it always looks like random words scrambled all over the place. It’s only fun if you’re the only person working with the code.


#28

Dueling syntaxes? At the other extreme, gofmt mechanically formats Go source code to the standard style, thus ending formatting battles.

@skyfold how about nim?


#29

:hand: Over my dead body!!1


#30

Nim uses a block: lable for just such an occasion.


#31

-1. Rust is not python.


#32

I’m a fan of Python’s blocking-by-indentation, and most of the arguments against it don’t hold up under scrutiny. That said, having a single syntax is more important than whether it’s braces, indentation or something else.

Once of the biggest problems with braces is that it gives license for ten thousand different formatting conventions. My vote would be to stick with braces but adopt strict formatting conventions (like Python’s PEP 8) and a ‘gofmt’-style tool to rigorously enforce them. This would give most of the ease-of-reading that whitespace indentation provides.


#33

I like significant indents and would additionally require the indent to be done with a single tab character.

That means that there is a single working way to format any piece of code, thus automatically enforcing syntax convention.

I’m not sure though if having Rust adopt this either as standard or as an option would be a good idea.


#34

-1000. Having debugged enough “what the fuck?!” bugs in Python, I never want to work with another indent-based language again. This idea works against the predictability and reliability that Rust otherwise aims for (and achieves).


#35

I’ve never had any problem you described with indent-based programming languages, I’d even say it works better than C’s system. Still, Rust with indents feels wrong, Rust currently aims to be similar to C/C++ in terms of syntax (where it’s possible).


#36

I’m gonna disagree on that statement, since I always keep my eyes on my code indentation, and it was not always was explaining itself. In order for code explain itself and no longer require comments, a programmer must name it’s variables, functions and etc in proper way. More on that you can find in Robert C. Martin’s “Clean Code Book”.


#37

I would imagine it would be done Haskell-style:

if true
    //do stuff
{
    let vec = vec![1,2,3]
    //do some work with vec, and then free it at the end of this scope
}

in other words, braces still exist when you need them, but they’re optional


#38

-1 As was said, sometimes you have to appease the borrow checker by adding blocks, as it is not quite smart enough to work out the lifetimes, making those hacks be whitespace dependent sounds like a nightmare


#39

+1 That is a good idea. But, there must a tool that would transcompile from indented style to the curlybraces style and vice versa. The transcompiled version must be readable. They must fully compatible.


#40

Okay, now I’m considering building the transcompiler a good way to go. I’ll take F# syntax as a basis and make an executable to parse it into rust code. What tools would you suggest me to build such thing?