aw1621107
Joined 9 years ago
Joined 9 years ago
And even without such a change, such a restriction is pretty easily worked around without a const reference with something like T&& temp = get_daily_prices(); sum(temp);. I don't think that'll result in different codegen than either passing a temporary to a const ref or allowing binding a temporary to a non-const ref.
My understanding was that const references don't provide that much in the way of optimization opportunities (if any?) due to aliasing and the existence of const_cast.
I'm not sure I can express it better than the proposal to add the corresponding feature [0]:
> This extension addresses the Romanian National Body's (NB) concern by enabling the writing of an individual contract assertion that guarantees the program execution will not continue past the contract assertion if it is violated, regardless of the semantics of other contract assertions. This addition to the C++26 Contracts facility allows reliable use of enforced contract assertions in large codebases with mixed contract semantics and also in hardened libraries.
> Is it worth the trade-offs?
Apparently some people/NBs feel quite strongly that it is.
> There's a reason why most code turns assert off in production and only does it in debug builds.
There's also a reason some codebases define asserts that aren't turned off in production in addition to debug-only asserts. The proposal lists multiple under Section 3.1 Existing Practice.
> and so if the code is trusted to work, you wouldn't want to check that it's working correctly.
Right, but sometimes you might think the consequences of such blind trust being misplaced are worse than the performance hit.
Static detection of impossible-to-satisfy contract conditions would be interesting, but as you say the technology is probably not ready for the kind of widespread use that inclusion in C++ would entail.
[0]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p39...
> Most languages are memory safe.
As I said, there's more to Rust than just memory safety. If memory safety were the only thing Rust offered then I think it wouldn't have garnered nearly as much attention as it has. It's memory safety on top of everything else that makes it as interesting as it is.
I think that period has arguably already come and gone. C and C++ have been the most popular systems languages for decades. I think it's clear that mere popularity is not sufficient to produce memory-safe software with those languages. If anything, those languages are experiencing somewhat of a renaissance these days and it's these more recent developments that end up producing improvements with regard to memory safety.
> I just wonder why don’t we just keep using C, C#, and Java? I mean the former two are memory safe, and they can do “unsafe” just like Rust.
Bit of a nitpick, but I think you meant "latter" instead of "former".
That aside, there's more to picking a language to use than just memory safety and the ability to perform "unsafe" operations. Take your pick from more technical reasons (zero-overhead FFI to C, low-level control over data structures/operations/runtime, etc.) and more "soft" reasons (language affordances/features, ecosystem, etc.).
> Another thing: why do we write pseudocode and not design languages to look like pseudocode?
Perhaps COBOL might be what you're looking for? :P
As with many things, language design is full of tradeoffs, particularly when you want to produce something that is not too unfamiliar to a certain audience. IIRC another factor is that languages that want to work well for "experts" will tend to favor different points on the design spectrum. Consider APL-family languages - those are completely incomprehensible to anyone not familiar with them, but that same terseness is highly valued by proficient practitioners and is something pseudocode-like (or even "regular" code-like!) could never replicate.
> The downside to using familiar syntax in a radically different language is that the differences really catch you by surprise.
Definitely agreed. It's a fine needle to thread for devs and I would not be surprised if there were tradeoffs to be made between familiarity and not importing too much of the inspiring language.
> I thought a lot about starting a blog to document the real stumbling points I hit, but it would be tedious and whiny, and I'm not interested in building an audience.
For what it's worth I personally think I'd find such a thing interesting to read. Sounds like it could make for some good feedback for better onboarding as well (well, assuming you're not talking about stumbling points from your first forays into Rust. But even those can still have some intellectual value, if the interest in Graydon's reflections on Rust's early days are any indication)
I suppose I'm a bit more optimistic than you; I feel that most commenters tend to do a pretty good job of not veering into unrelated topics, even when things start getting heated. The lobste.rs discussion on this post, for instance, seems to have avoided these tangents.
> As for strangeness, all three of these are valid:
To mangle a quote from a certain movie, "The strangeness budget is more what you'd call 'guidelines' than actual rules" :P
I agree that most C++ programmers would expect what you showed, but as with many things the turbofish is a tradeoff (unfamiliar syntax vs. ease of parsing). I presume the Rust devs at the time thought the tradeoff was worth it and there just hasn't been sufficient consensus/motivation to change it since then.
I recall someone here talking about the oddity you mention, but I don't remember if they said it was an implementation quirk or something more fundamental to Rust's grammar.
That being said...
> For instance, why copy C++'s "::" for paths when Rust was a greenfield programming language without the burden of backwards compatibility?
The concept of a strangeness budget [0] is probably relevant here - if you want to appeal to a particular audience (C++ programmers in this case), you probably want to limit "strangeness" to places where it's likely to make the largest impact. From this lens it might be interesting to look at your question from the other direction: What would the benefit be of not copying C++'s syntax in this context, if any, and is that worth spending part of your strangeness budget?
Also this comment [1] from a few days ago seems well-timed, though it's answering a slightly different question:
> The `::` syntax allows the compiler to identify and resolve all the static paths before any type inference or other analysis. This is important because macros are expanded between these stages, and can in turn introduce more static names which must also be resolved before any further compilation.
Unfortunately I haven't been able to find an authoritative explanation of why :: instead of something else, though given how old it is [2] I'd guess it's one of those strangeness budget decisions.
> ("= Expression except LazyBooleanExpression or end with a }").
Based on the syntax diagram this bit is for let-else. If you look at the RFC [3] you'll get the answer you seek:
> While allowing e.g. if {} else {} directly in the expression position is technically feasible this RFC proposes it be disallowed for programmer clarity so as to avoid ... else {} else {} situations as discussed in the drawbacks section. Boolean matches are not useful with let-else and so lazy boolean expressions are disallowed for reasons noted in future-possibilities. These types of expressions can still be used when combined in a less ambiguous manner with parentheses, which is allowed under the two expression restrictions.
> Turbofish, could probably have been avoided by not using "<" and ">" both as type argument delimiters and as an operator for comparison. Scala 2 for example does it differently, using "[" and "]" for type argument delimiting.
Rust did in fact use square brackets for generics early on, but that was later changed to angle brackets [4, 5]. I haven't been able to dig up explicit discussions as to why, though again I suspect strangeness budget concerns were at least part of the reason.
Using square brackets for types is probably not a change that can be made in isolation either; at a minimum you'd need to consider how that interacts with indexing/function calls/etc. and how those in turn interact with traits (e.g., if manual implementation of function traits is ever stabilized).
Also, from what I understand the turbofish was never strictly necessary, and in fact it actually is possible for Rust to eliminate the need for the turbofish thanks to the magic of editions ([6]). It's "just" a matter of someone taking up the cause and gathering sufficient consensus.
> Using two different operators for what one could call "type annotations", namely both ":" and "->", in different cases, feels ugly to me.
You might find this email from Graydon Hoare interesting [7]. Among other things he says:
> If the argument's about consistency, I think the consistent thing to do is to keep ":" as a separator between bindings and types. My eyes get a little fuzzy guessing the associativity of "let x : fn(T) : U;" in a way that they do not when reading "let x : fn(T) -> U;". But I suppose reasonable people could disagre, or phrase the consistency requirement differently. My math professors would cry.
[0]: https://steveklabnik.com/writing/the-language-strangeness-bu...
[1]: https://news.ycombinator.com/item?id=50369228
[2]: https://web.archive.org/web/20130118053116/http://static.rus...
[3]: https://rust-lang.github.io/rfcs/3137-let-else.html
[4]: https://github.com/rust-lang/rust/commit/70b009178cf050d5bb7...
[5]: https://github.com/rust-lang/rust/commit/0107d6a81d2180d3a1e...
[6]: https://github.com/rust-lang/rfcs/pull/2544
[7]: https://web.archive.org/web/20201111221825/https://mail.mozi...
> This is not a memory safety vulnerability; rather, it is one of those issues that, people often say, languages with better type systems won’t save you from. This is true! You can very well write a Rust function that returns "" if the password is too long. But the big difference between C and Rust in this regard is that the norms of the Rust community do not allow that.*
...He has the power to do this? And for which elections, exactly? The main go/no-go votes are the ones at the big meetings and I'd imagine there's not a lot of room for scheduling shenanigans there.
Say what!?
The only red-string-on-a-cork-board I can connect is between guaranteed enforcement and the design decision that "contract assertions must not be used for error handling and input validation". The latter would seem to rule out guaranteed enforcement now and in the future and EWG apparently voted against P3911 back in January [0], so perhaps they think that they won't ever see guaranteed enforcement if contracts is left in the standard as-is.
That's all quite speculative, though, so take that with an appropriately-sized grain of salt/
[0]: https://github.com/cplusplus/papers/issues/2536#:~:text=Dire...