3 ms·
Interestingly, D has this and everybody considers it great. The community consensus is that pure should have been the default, but it is not changed for backwar
by qznc 13y ago
Interestingly, D has this and everybody considers it great. The community consensus is that pure should have been the default, but it is not changed for backwards compatibility.
http://qznc.github.io/d-tut/idiomatic.html#purity http://qznc.github.io/d-tut/idiomatic.html#purity
- frozenport 13y agoYou would only use D if you considered it great.
- Tuna-Fish 13y agoThere are other reasons to use D, and indeed, most of the community came from languages without pure functions and did not want the feature before trying it. However, it's one of those things where once you use it for a while, you begin to really appreciate it. Better optimization, autovectorization and cpu -> gpu code conversion are all unimportant reasons for checked purity in the type system. The important reason is that by constraining what functions do, it makes reasoning about complex systems easier. Generally, after you've worked in maintenance on one large project that designed everything to be pure unless there's a very compelling reason not to, you start to really miss the feature when you go back to projects in languages without it.
- catnaroek 13y agoAgreed there. But, on the other hand, D misses out on statically checked resource management, and IMO that is more important than purity in the niche Rust and D are targeted at (systems development). Of course, nothing prevents a language from having both. :-)
- WalterBright 13y agoPurity has a large advantage when writing concurrent code, as pure functions don't need to be synchronized. This is why D has enforced purity (via an attribute) rather than inferred purity.
- kibwen 13y agoD and Rust are vastly different beasts. Here's why none of the arguments from that page apply to Rust: > Pure means your function does not use or change mutable > global state except what is available from the > arguments. If in addition the arguments are not > mutable, a function is called “strongly pure”. In Rust, immutability is the default, and you must always opt in to mutability. A function's type signature will always make it apparent as to which arguments are mutable and which are not. If you declare a variable as mutable but then never mutate it, the compiler will pester you to make it immutable. Furthermore, Rust is so hell-bent against global mutable state that even reading global state that is declared as mutable requires one to wrap the code in an `unsafe {}` block. > Nothrow means your function will never throw an > exception. Rust does not have exceptions, and prefers to use returned algebraic datatypes and non-unwinding conditions to handle errors. > Safe means your function cannot break the type system > via unsafe casts or inline assembly. As alluded to earlier, all functions in Rust are safe by default. An explicit `unsafe {}` block is required to do stuff like inline assembly and type transmutation, and functions themselves can be marked as `unsafe` in order to force callers to use an unsafe block when calling them.