Get pointer or reference of an element in Array(struct)
David Nadlinger
code at klickverbot.at
Sat Dec 9 19:26:26 UTC 2017
On Saturday, 9 December 2017 at 06:46:27 UTC, Arun Chandrasekaran
wrote:
> Thanks. Just curious why reference can't be obtained here.
> Saves nasty null checks in most places.
D simply doesn't have a (C++-style) concept of references as part
of the type. Arguments can be passed by reference - hence the
`ref` keyword -, but "free" references don't exist in the
language.
The ref in foreach loop variables can be conceptually thought of
as a parameter to the loop body as well. (For opApply-based
iteration, the loop body indeed gets turned into a function; for
"plain" iteration the compiler AST internally has special ref
variables, but they are not visible to the language.)
-David
More information about the Digitalmars-d-learn
mailing list