If you could make any changes to D, what would they look like?
harakim
harakim at gmail.com
Sat Oct 30 08:53:21 UTC 2021
On Wednesday, 20 October 2021 at 09:47:54 UTC, SealabJaster wrote:
> Just for giggles, without pesky things like breaking changes;
> rational thinking, logical reasoning behind the changes, etc.
>
> What interesting changes would you make to the language, and
> what could they possibly look like?
>
> Here's a small example of some things I'd like.
>
> ```d
> import std;
>
> interface Animal
> {
> void speak(string language);
> }
>
> struct Dog
> {
> @nogc @nothrow @pure @safe
> static void speak(string l)
> {
> // Pattern matching of some kind
> // With strings this is just a fancy switch statement,
> but this is the gist of it
> match l with
> {
> "english" => writeln("woof"),
> "french" => writeln("le woof"),
> _ => writeln("foow")
> }
> }
> }
>
> struct Cat
> {
> // Remove historical baggage. Make old attributes into `@`
> attributes
> @nogc @nothrow @pure @safe
> static void speak()
> {
> writeln("meow")
> }
> }
>
> // ? for "explicitly nullable"
> void doSpeak(alias T)(string? language)
> if(is(T == struct) && match(T : Animal)) // Match structs
> against an interface.
> {
> // immutable by default. ?? is the same as in C#
> auto lang = language ?? "UNKNOWN";
>
> // So of course we'd need a mutable keyword of some sort
> mutable output = $"{__traits(identifier, T)} speaking in
> {lang}"; // String interpolation
> writeln(output);
> T.speak(lang);
> }
>
> void main()
> {
> doSpeak!Dog;
> doSpeak!Cat; // Should be a compiler error since it fails
> the `match` statement
> }
> ```
* I would probably choose a different syntax for templates.
* I would add ?. and ??
* I would work on the levels of strictness. D definitely does
this better than any other language, but I have used it and found
it wanting from time to time. Most projects start as a prototype
and you don't need a lot of strict rules for that. For that you
don't want type checking or method signature validation, you want
a scripting language. As you get going, you want to still move
fast but you probably want a strongly-typed language and some
other features. At some point, certain parts of the code,
libraries or even whole programs can be battened down and you
want features like compile-time guarantees and performance. I
would say D is the leader in this for all the languages that I
use, but I think it could have a little more room to grow and
have better documentation.
* Possibly add something to help autocomplete. For example, in
C#, extension methods get picked up by autocomplete. That is the
advantage they have over normal methods.
More information about the Digitalmars-d
mailing list