6 ms·
Could you, please, elaborate. In my opinion zero-cost abstraction is nothing but a catchy meme. Could you provide an example, so I would try to implement such
by lngnmn 10y ago
Could you, please, elaborate. In my opinion zero-cost abstraction is nothing but a catchy meme.
Could you provide an example, so I would try to implement such a wonder in one of classic languages I know, like SML or Scheme?
- steveklabnik 10y agoA simple one is Option. An Option<&T> is a pointer to some type T. Option is an enum, & is a reference, which has all of Rust's safety checking. This will all compile down to a regular old pointer, just like in C. Even though you have null checking and protection against dangling pointers, etc. It's all at compile time, zero run-time overhead.
- lngnmn 10y agoSo, typed pointers with an analog of the option type. Thank you. Could we say that Swift or even SML with typed refs has similar safety and zero-cost abstractions?
- steveklabnik 10y agoWhile they may have some features that are zero-cost, it's not a defining feature of either language, and so they don't take it as far as Rust does. This was only one example, there are many more that those languages do not do, for various reasons.
- masklinn 10y agoSimilar safety, yes absolutely. Zero-cost abstraction not that I know of although I believe Swift has pretty good support for them and the long-term goal is to be in the same space as Rust (https://news.ycombinator.com/item?id=13077178 https://news.ycombinator.com/item?id=13077178)
- kibwen 10y agoDetermining whether an abstraction is zero-cost or not is often buried deep in a language's implementation, so you'd need an expert to determine whether a given abstraction in Swift or SML qualifies. But regarding closures specifically, I can say that it's very unlikely that either Swift or SML have a zero-cost implementation, since having stack-allocated closures is an immense and deliberate decision in language design (AFAIK only C++ and Rust do this).
- vvanders 10y agoHere's a good example, fresh off of the first result from Google: https://ruudvanasseldonk.com/2016/11/30/zero-cost-abstractions https://ruudvanasseldonk.com/2016/11/30/zero-cost-abstractio...
- tetromino_ 10y ago> In my opinion zero-cost abstraction is nothing but a catchy meme. It has a specific meaning: an abstraction which compiles to the same machine code as a low-level, unsafe, non-generic C-like implementation would. Without any additional pointer accesses or sanity checks required at runtime. The canonical example would be an iterator over a vector which compiles to the same machine code as a traditional C for loop.