digitalmars.D - More on Rust
- bearophile (34/36) Feb 09 2011 The Development of the Rust language from Mozilla and Graydon Hoare is g...
- so (5/21) Feb 09 2011 Already love it, lowercase ftw!
- so (1/2) Feb 09 2011 Oh '+' is string concat in Rust, and log is not that bad either!
- Nick Sabalausky (6/13) Feb 09 2011 That would be really nice to have in D. There's been many times I've nee...
- spir (11/26) Feb 10 2011 I don't like that at all. The second main feature of staticity if self-d...
- Jean Crystof (11/34) Feb 10 2011 How about this?
- Jesse Phillips (11/24) Feb 10 2011 Humm, what we currently have:
- Andrej Mitrovic (6/10) Feb 10 2011 auto is great for rapid prototyping. And so are templated functions.
- Walter Bright (2/14) Feb 10 2011 auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";
- Andrej Mitrovic (53/54) Feb 10 2011 Aye, a one liner!
- Christopher Nicholson-Sauls (18/70) Feb 11 2011 Even better:
- Andrej Mitrovic (2/19) Feb 11 2011 Damn I didn't know that! Thanks.
- Jacob Carlborg (12/26) Feb 10 2011 For this simple if statement it works but as soon you have a few lines
- Jim (10/39) Feb 10 2011 Other languages may have bloated syntaxes, with no particular benefit.
- Jean Crystof (2/45) Feb 11 2011 You're right. ? : is a masterpiece among syntaxes. Maybe a bit too terse...
- Jacob Carlborg (4/43) Feb 11 2011 And that was the first thing I said one could do.
- spir (23/62) Feb 11 2011 Agreed.
- Jim (4/71) Feb 11 2011 I always make a conscious effort to write succinct expressions. My rule ...
- Bruno Medeiros (14/15) Feb 23 2011 Hum, this typestate concept actually looks quite interesting. I need to
The Development of the Rust language from Mozilla and Graydon Hoare is going on. It will probably become a quite interesting system language, quite more interesting than Go. One of the most interesting features of Rust will be its typestate. Rust has structural typing too, this is sometimes useful (but to use it well you need something like pattern matching used in functional languages like ML, that I don't know if is present in Rust). Graydon is following D idea of putting a basic unit testing feature in the language: https://mail.mozilla.org/pipermail/rust-dev/2010-December/000160.html https://mail.mozilla.org/pipermail/rust-dev/2010-December/000163.html https://mail.mozilla.org/pipermail/rust-dev/2010-December/000165.html Rust also puts basic logging as a built-in feature. This is a blog post about Rust type inference: http://pcwalton.blogspot.com/2010/10/rust-features-i-type-inference.html Rust doesn't have a whole program type inferencer (as ML), like a Hindley-Milner algorithm, to keep things simple and allow separate compilation (and probably to keep compilation times low, etc). So you have to give type to function arguments, and if you want you are free to use "auto" everywhere inside the function to infer types locally. I think this is good. So on this it's quite similar to D, but there are some differences. Rust allows to do this ("log" is a built-in logging feature, I don't know how they call the logarithm function): auto x; if (localtime().hours >= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x; From the blog post:Rust features what I call feed-forward type inference, which is summed up by this rule: Variables have no type until they are assigned a value, at which point their type can't change.<Here, we didn't initialize x in its declaration, but this code still compiles and runs properly. This is because Rust is able to wait until x is assigned to variables must be assigned a value at declaration time), Rust doesn't require that auto declarations be initially assigned a value to determine a type for them. At the same time, unlike C, the compiler will emit an error if you ever forget to assign a value to the variable in any branch of your code. For instance, if you omit the else branch above, the code won't compile.<Rust is statically typed, so this is an error, it's not possible (unless you use some kind of variant): log "do you want to add strings or ints?"; auto use_str = input.readline == "strings"; if (use_str) { a = "hello "; b = "world!"; } else { a = 1; b = 1; } log a + b; Both branches of the "if" must assign the same types to a and b, like when in D you use an auto return type, and the D compiler makes sure all your return statements return exactly the same type. "Variables have no type until they are assigned a value, at which point their type can't change." looks meaningless if you see types as timeless things, that are are always true and immutable for a variable. But in Rust there are typestates, so while a variable can't change type, its type sometimes changes state along the flow of the code, such state of the type may be different in different parts of the code. Bye, bearophile
Feb 09 2011
Rust is statically typed, so this is an error, it's not possible (unless you use some kind of variant):Not quite clear in the example but just add an extra line after if block.log "do you want to add strings or ints?"; auto use_str = input.readline == "strings"; if (use_str) { a = "hello "; b = "world!"; } else { a = 1; b = 1; } auto c = b - a; // wah? log a + b; ... Bye, bearophileAlready love it, lowercase ftw! Although built-in logging is nice, both the name and the operator of logging are very bad choices. Especially for a standard library.
Feb 09 2011
both the name and the operator of logging are very bad choices.Oh '+' is string concat in Rust, and log is not that bad either!
Feb 09 2011
"bearophile" <bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x; if (localtime().hours >= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D. There's been many times I've needed to move a declaration up one scope from its initialization and was therefore forced to get rid of the "auto". Not a major problem obviously, but that sure would be nice.
Feb 09 2011
On 02/10/2011 06:43 AM, Nick Sabalausky wrote:"bearophile"<bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...I don't like that at all. The second main feature of staticity if self-doc code. How is one, as a reader, supposed to guess what x is and means? I'm not a great fan of auto, neither, use it would happily live w/o it, except for functions operating on ranges, that return types coming from who-knows where (certainly another planet). Denis -- _________________ vita es estrany spir.wikidot.comauto x; if (localtime().hours>= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D. There's been many times I've needed to move a declaration up one scope from its initialization and was therefore forced to get rid of the "auto". Not a major problem obviously, but that sure would be nice.
Feb 10 2011
spir Wrote:On 02/10/2011 06:43 AM, Nick Sabalausky wrote:How about this? spir Wrote: auto x = if (localtime().hours>= 8) { "awake!" } else { "asleep, go away." }; log "I'm " + x; I think it solves the problem of unkown type for x nicely. And it's very close to what we already have. something ? foo : bar might do the same, but if() [} else {} is much more readable IMO."bearophile"<bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...I don't like that at all. The second main feature of staticity if self-doc code. How is one, as a reader, supposed to guess what x is and means? I'm not a great fan of auto, neither, use it would happily live w/o it, except for functions operating on ranges, that return types coming from who-knows where (certainly another planet).auto x; if (localtime().hours>= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D. There's been many times I've needed to move a declaration up one scope from its initialization and was therefore forced to get rid of the "auto". Not a major problem obviously, but that sure would be nice.
Feb 10 2011
Jean Crystof Wrote:How about this? spir Wrote: auto x = if (localtime().hours>= 8) { "awake!" } else { "asleep, go away." }; log "I'm " + x; I think it solves the problem of unkown type for x nicely. And it's very close to what we already have. something ? foo : bar might do the same, but if() [} else {} is much more readable IMO.Humm, what we currently have: auto x = { if (localtime().hours>= 8) { "awake!" } else { "asleep, go away." }; }(); log "I'm " + x;
Feb 10 2011
On 2/10/11, spir <denis.spir gmail.com> wrote:I'm not a great fan of auto, neither, use it would happily live w/o it, except for functions operating on ranges, that return types coming from who-knows where (certainly another planet).auto is great for rapid prototyping. And so are templated functions. After I'm satisfied with the functionality of my code I usually replace much of my use of auto with the actual type (mostly variable declarations). auto is still useful in templates, where writing a return type can be complicated.
Feb 10 2011
Nick Sabalausky wrote:"bearophile" <bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";auto x; if (localtime().hours >= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D.
Feb 10 2011
On 2/10/11, Walter Bright <newshound2 digitalmars.com> wrote:auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";Aye, a one liner! I hate seeing things like this: if (funcall()) { var = "foo"; } else { var = "bar"; } So much clutter instead of using the simple: var = funcall() ? "foo" : "bar"; I also see this sometimes: auto var = funcall(); if (var == "foo" || var == "bar" || var == "foobar" || var == "barfoo") { // some complex code } else if (var == "blue" || var == "green") { // some complex code } else if ()// more and more code.. { } But not many people seem to know that D supports strings in case statements: switch(funcall()) { case "foo": case "bar": case "foobar": case "barfoo": { // complex code } break; case "blue": case "green": { // complex code } break; default: } Not everybody will like this since it wastes a lot of vertical space. But at least it fits in 80 columns! (:p). Plus you don't need to create a temporary variable. The cool thing is the compiler will warn you if you miss out setting the default switch, which is something you won't get with if/elseif's. The final switch statement is even better when using enums. Added another enum member, but forgot to update the final switch statement? Boom, compiler error! It's a great thing for avoiding bugs imo.
Feb 10 2011
On 02/10/11 13:49, Andrej Mitrovic wrote:On 2/10/11, Walter Bright <newshound2 digitalmars.com> wrote:Even better: switch( funcall() ) { case "foo", "bar", "foobar", "barfoo": { // complex code break; } case "blue", "green": { // complex code break; } default: // do nothing -- i like to comment defaults } Also often forgotten, that 'case' clauses take an argument list, not just an expression. And yeah, in this case at least... it still fits in 80 columns. (I prefer 90 myself, but it's moot.) -- Chris N-Sauto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";Aye, a one liner! I hate seeing things like this: if (funcall()) { var = "foo"; } else { var = "bar"; } So much clutter instead of using the simple: var = funcall() ? "foo" : "bar"; I also see this sometimes: auto var = funcall(); if (var == "foo" || var == "bar" || var == "foobar" || var == "barfoo") { // some complex code } else if (var == "blue" || var == "green") { // some complex code } else if ()// more and more code.. { } But not many people seem to know that D supports strings in case statements: switch(funcall()) { case "foo": case "bar": case "foobar": case "barfoo": { // complex code } break; case "blue": case "green": { // complex code } break; default: }
Feb 11 2011
On 2/11/11, Christopher Nicholson-Sauls <ibisbasenji gmail.com> wrote:Even better: switch( funcall() ) { case "foo", "bar", "foobar", "barfoo": { // complex code break; } case "blue", "green": { // complex code break; } default: // do nothing -- i like to comment defaults } Also often forgotten, that 'case' clauses take an argument list, not just an expression. And yeah, in this case at least... it still fits in 80 columns. (I prefer 90 myself, but it's moot.) -- Chris N-SDamn I didn't know that! Thanks.
Feb 11 2011
On 2011-02-10 20:15, Walter Bright wrote:Nick Sabalausky wrote:For this simple if statement it works but as soon you have a few lines in the if statement it will become really ugly. But one could wrap the if statement in a function instead. In other languages where statements really are expressions this works: auto x = if (localtime().hours >= 8) "awake!"; else "asleep, go away."; log "I'm " + x; -- /Jacob Carlborg"bearophile" <bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";auto x; if (localtime().hours >= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D.
Feb 10 2011
Jacob Carlborg Wrote:On 2011-02-10 20:15, Walter Bright wrote:Other languages may have bloated syntaxes, with no particular benefit. auto x = localtime().hours >= 8 ? "awake!" : "asleep, go away."; log( "I'm " ~ x ); If the expressions are complex I put them in functions. 1) It hides and isolates details, which allows you to focus on the more abstract aspects. 2) It gives the expression a name and facilitates reuse.Nick Sabalausky wrote:For this simple if statement it works but as soon you have a few lines in the if statement it will become really ugly. But one could wrap the if statement in a function instead. In other languages where statements really are expressions this works: auto x = if (localtime().hours >= 8) "awake!"; else "asleep, go away."; log "I'm " + x;"bearophile" <bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";auto x; if (localtime().hours >= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D.
Feb 10 2011
Jim Wrote:Jacob Carlborg Wrote:You're right. ? : is a masterpiece among syntaxes. Maybe a bit too terse, but it does exactly what people expect it to do and it has existed since C was born.On 2011-02-10 20:15, Walter Bright wrote:Other languages may have bloated syntaxes, with no particular benefit. auto x = localtime().hours >= 8 ? "awake!" : "asleep, go away."; log( "I'm " ~ x );Nick Sabalausky wrote:For this simple if statement it works but as soon you have a few lines in the if statement it will become really ugly. But one could wrap the if statement in a function instead. In other languages where statements really are expressions this works: auto x = if (localtime().hours >= 8) "awake!"; else "asleep, go away."; log "I'm " + x;"bearophile" <bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";auto x; if (localtime().hours >= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D.
Feb 11 2011
On 2011-02-11 08:39, Jim wrote:Jacob Carlborg Wrote:And that was the first thing I said one could do. -- /Jacob CarlborgOn 2011-02-10 20:15, Walter Bright wrote:Other languages may have bloated syntaxes, with no particular benefit. auto x = localtime().hours>= 8 ? "awake!" : "asleep, go away."; log( "I'm " ~ x ); If the expressions are complex I put them in functions. 1) It hides and isolates details, which allows you to focus on the more abstract aspects. 2) It gives the expression a name and facilitates reuse.Nick Sabalausky wrote:For this simple if statement it works but as soon you have a few lines in the if statement it will become really ugly. But one could wrap the if statement in a function instead. In other languages where statements really are expressions this works: auto x = if (localtime().hours>= 8) "awake!"; else "asleep, go away."; log "I'm " + x;"bearophile"<bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x = (localtime().hours>= 8) ? "awake!" : "asleep, go away.";auto x; if (localtime().hours>= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D.
Feb 11 2011
On 02/11/2011 08:39 AM, Jim wrote:Jacob Carlborg Wrote:Agreed. But in practice I often end up beeing dubious about seemingly nice features like, precisely, the ternary operator. In languages that do not have such a nicety people end up writng eg: if (localtime().hours>= 8) x = awake; else x = "asleep, go away."; Apart from the very mild annoyance, I guess, of writing twice "x =", this is all gain: code is both more compact and legible. Precisely, because the ternary operator is a bit weird syntactically (and not that commonly needed and used in real code), people feel, just like you, the need to clarify it in code, finally using more vertical space. Note that the case here is the simplest possible, expressions being plain literal constants. I personly only consume 3 lines: auto x = (localtime().hours>= 8) ? "awake!" : "asleep, go away."; What do you think? Denis -- _________________ vita es estrany spir.wikidot.comOn 2011-02-10 20:15, Walter Bright wrote:Other languages may have bloated syntaxes, with no particular benefit. auto x = localtime().hours>= 8 ? "awake!" : "asleep, go away."; log( "I'm " ~ x ); If the expressions are complex I put them in functions. 1) It hides and isolates details, which allows you to focus on the more abstract aspects. 2) It gives the expression a name and facilitates reuse.Nick Sabalausky wrote:For this simple if statement it works but as soon you have a few lines in the if statement it will become really ugly. But one could wrap the if statement in a function instead. In other languages where statements really are expressions this works: auto x = if (localtime().hours>= 8) "awake!"; else "asleep, go away."; log "I'm " + x;"bearophile"<bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x = (localtime().hours>= 8) ? "awake!" : "asleep, go away.";auto x; if (localtime().hours>= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D.
Feb 11 2011
spir Wrote:On 02/11/2011 08:39 AM, Jim wrote:I always make a conscious effort to write succinct expressions. My rule is to only use the ternary operator when it actually makes the code cleaner. Why? Branches should be conspicuous because they increase the number of code paths. Exceptions are the other big source of code paths. This is, btw, why I think that scope statements in D are just superb.Jacob Carlborg Wrote:Agreed. But in practice I often end up beeing dubious about seemingly nice features like, precisely, the ternary operator. In languages that do not have such a nicety people end up writng eg: if (localtime().hours>= 8) x = awake; else x = "asleep, go away."; Apart from the very mild annoyance, I guess, of writing twice "x =", this is all gain: code is both more compact and legible. Precisely, because the ternary operator is a bit weird syntactically (and not that commonly needed and used in real code), people feel, just like you, the need to clarify it in code, finally using more vertical space. Note that the case here is the simplest possible, expressions being plain literal constants. I personly only consume 3 lines: auto x = (localtime().hours>= 8) ? "awake!" : "asleep, go away."; What do you think?On 2011-02-10 20:15, Walter Bright wrote:Other languages may have bloated syntaxes, with no particular benefit. auto x = localtime().hours>= 8 ? "awake!" : "asleep, go away."; log( "I'm " ~ x ); If the expressions are complex I put them in functions. 1) It hides and isolates details, which allows you to focus on the more abstract aspects. 2) It gives the expression a name and facilitates reuse.Nick Sabalausky wrote:For this simple if statement it works but as soon you have a few lines in the if statement it will become really ugly. But one could wrap the if statement in a function instead. In other languages where statements really are expressions this works: auto x = if (localtime().hours>= 8) "awake!"; else "asleep, go away."; log "I'm " + x;"bearophile"<bearophileHUGS lycos.com> wrote in message news:iivb5n$na3$1 digitalmars.com...auto x = (localtime().hours>= 8) ? "awake!" : "asleep, go away.";auto x; if (localtime().hours>= 8) { x = "awake!" } else { x = "asleep, go away." } log "I'm " + x;That would be really nice to have in D.
Feb 11 2011
On 10/02/2011 00:23, bearophile wrote:But in Rust there are typestates, so while a variable can't change type, its type sometimes changes state along the flow of the code, such state of the type may be different in different parts of the codeHum, this typestate concept actually looks quite interesting. I need to look in more detail, but it seems similar to some ideas I roughly considered several times before, about having a language being able to track which assertions are true for a given variable/value at any (or most) given points in code. I'm fond of ideas that could make a static type system even more powerful (more checking capabilities), without making it significantly more obtuse or verbose. I wonder if many improvements could be made on this area, enough even for it to be considered a significant step forward in language design/evolution. Oh how I wish that a day would have 48 hours... -- Bruno Medeiros - Software Engineer
Feb 23 2011