Worst ideas/features in programming languages?

Petar Petar
Wed Dec 29 08:10:42 UTC 2021


On Wednesday, 29 December 2021 at 05:09:33 UTC, Walter Bright 
wrote:
> On 11/9/2021 11:09 PM, Timon Gehr wrote:
>> On 10/16/21 1:12 AM, Walter Bright wrote:
>>> On 10/12/2021 2:38 PM, Timon Gehr wrote:
>>>> - non-lexical variable lifetimes (probably not happening)
>>>
>>> It's already implemented for @live.
>> 
>> 
>> Specifically, I would like it to influence scoping.
>> 
>> ```d
>> int x=2;
>> int y=move(x);
>> int z=x; // error: undefined identifier x (was moved away)
>
> @live currently does that for pointers, but not for 
> non-pointers. What's the use case for it?
>

The typestate [1] design pattern. In languages that support some 
form of affine types [2] like Rust (*), this design pattern can 
be used to prevent classes of programmer errors at compile-time, 
while the best C++ and D can do is use `assert` at run-time to 
facilitate detecting them. Here's a few articles that showcase 
this in Rust:

* https://cliffle.com/blog/rust-typestate/
* 
https://rustype.github.io/notes/notes/rust-typestate-series/rust-typestate-part-1
* https://docs.rs/typestate/latest/typestate/

(*) Technically, affine types are about using a variable at most 
once. In Rust, if a type doesn't implement the Copy / Clone 
traits [3], variables of that type have move semantics - that is, 
they can't be used after they have been passed to a function that 
takes them by value (passing ownership). But they can be still 
passed multiple times to functions take them by reference 
(borrowing).

[1]: https://en.wikipedia.org/wiki/Typestate_analysis
[2]: https://en.wikipedia.org/wiki/Substructural_type_system
[3]: https://doc.rust-lang.org/std/marker/trait.Copy.html




More information about the Digitalmars-d mailing list