Is D programming friendly for beginners?
Quirin Schroll
qs.il.paperinik at gmail.com
Tue Jul 23 10:59:33 UTC 2024
On Monday, 4 March 2024 at 13:37:53 UTC, Fidele wrote:
> Is D programming friendly for beginners?
Only if you use `@safe` on your `main` function and essentially
everywhere except templates (those infer `@safe` and you should
let them do that). `@system` is an expert tool, unfortunately,
it’s the default. Unless you use `@safe`, it is actually easy to
accidentally corrupt memory. For short, `@safe` disallows any
operation that could introduce undefined behavior, in particular
memory corruption.
There are so-called preview switches on the compiler. Those
enable latest features or bugfixes that are incompatible with the
old langue state. If a preview feature is deemed ready for
production, it’ll become active by default. IMO, the following
preview switches should be enabled:
* `-preview=dip1000` This allows more operations in `@safe` code,
but error messages can become more cryptic. E.g., pre-DIP1000,
taking the address of a local variable was illegal and with
DIP1000, it’s allowed under certain circumstances, but when it’s
not okay, the error message isn’t simply “you can’t do that”. It
will become the default at some point, so it might be worth to
learn its semantics from the beginning.
* `-preview=dip1008` Allows allocating `Error` objects in `@nogc`
code. Makes `@nogc` code more useful.
* `-preview=fieldwise` Must have. Fixes auto-generated
comparisons for structs.
* `-preview=fixAliasThis` Fixes lookup of `alias this` constructs.
* `-preview=nosharedaccess` Must have. Fixes `shared`, which can
be unsafe otherwise.
* `-preview=in` Makes the parameter storage class `in` actually
useful. Note: This is dangerous without `@safe`. Under the
preview switch, `in` makes strong guarantees, but without
`@safe`, it is assumed, but not checked, if the guarantees truly
hold. With `@safe`, however, they are checked and it’s fool-proof.
* `-preview=inclusiveincontracts` Must have. Fixes function
contracts.
* `-preview=fixImmutableConv` Must have. Fixes various conversion
issues in the type system.
Not actively recommended, but not actively harmful either:
* `-preview=bitfields` Unclear if it’s going to stay.
* `-preview=systemVariables` Primarily relevant if you write
`@system` code. First learn the language. (No harm if enabled.)
Recommended to stay away from:
* `-preview=rvaluerefparam` Don’t use it. It’s never going to be
the default and likely will be removed in the future in favor of
something else.
The reason I’m advocating preview switches is because as a
learner, you’re likely not writing production code that’s going
to stay, but throwaway code for the purpose of you gaining
knowledge of the language.
More information about the Digitalmars-d-announce
mailing list