`in` on function parameters: const, scope const, ref scope const ?
Mathias LANG
geod24 at gmail.com
Fri Apr 3 06:19:38 UTC 2020
There's been a discussion recently about `-preview` switches
(https://forum.dlang.org/thread/r627m8$2p6c$1@digitalmars.com).
It turns out one of the preview switch that is to be introduced
next release is `-preview=in`, meant to make `in` on parameters
(not to be confused with opIn) means `scope const` instead of
just `const`.
Now I wasn't particularly fond of this PR, since it doesn't seem
logical to me to have 2 switches for the same thing (`in` was
changed to mean `const` when DIP1000 was a PR, aiming to reduce
the breakage, but before it was behind a switch, which completely
nullified the need for changing it in the first place).
But since leadership decided to go with the switch anyway, why
not go all the way and make it means what it was actually
supposed to mean in the first place ? My experience with D is
that I had to stick quite a few `const ref scope` (in varying
order) on my parameters to mean "this is an input parameter. I'm
going to read it, not modify it. And I will not keep a reference
to it, because I just need it for the processing of this
function". However I couldn't use `in` because it would pass
things by value, meaning my `struct`s would get blitted over and
over.
I have submitted a PR for it:
https://github.com/dlang/dmd/pull/11000 and would like to know
how other users of `in` expect it to behave, or would want it to
behave.
Bear in mind this is a `-preview` switch: it won't break your
code.
One of the downside I'm aware of is passing rvalue. But luckily,
we got another preview for this (`-preview=rvaluerefparam`).
More information about the Digitalmars-d
mailing list