simple definition of "in" function parameters, and default "ref"

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Oct 27 23:47:06 UTC 2018


On Saturday, October 27, 2018 4:32:48 PM MDT 12345swordy via Digitalmars-d 
wrote:
> The "in" feature perplex me here, I never understand why we
> couldn't simply write "const scope" instead of "in". Seems it
> better off to mean "(foreach var a in x)" for readability sake
> instead of using as a parameter that no-one seems to be hardly
> using.

As I understand it, the in keyword was used on parameters in D1 to mean
something something similar to const, but whatever it meant, it wasn't
something that was going to match up with D2. And for D2, it was decided to
make it mean const scope in an attempt to make porting code from D1 to D2
easier. However, with everything going on early in D2 development (and in
the years since), scope was not exactly a priority, and so it wasn't really
fully sorted out and was never focused on until Walter worked on DIP 1000,
meaning that while in was supposed to mean const scope, it never really
properly did. It did for delegates, so it wasn't identical to const but was
effectively identical to const everywhere else making it just plain weird in
general. Personally, I think that having a keyword mean two other keywords
is a terrible idea in the first place, but there are definitely folks who
like the idea.

If in were added to D2 without its history in D1, I expect that the
situation would have been quite different. Regardless, none of that would
have had any effect on foreach, because in is already used as a keyword
elsewhere in the language and in places similar to foreach - e.g. it's an
operator.

if(auto value = key in aa)

and that usage arguably clashes far more with the idea of using in with
foreach than having used it with function parameters. Also, whether using in
with foreach would have been easier to read is highly debatable. Personally,
I find in harder to read, not easier. If anything, I would have expected the
debate to have been over whether it should have been a colon instead of a
semicolon, since IIRC most other major C-derived languages have gone with
the colon, whereas D went with the semicolon. But in the end, it's mostly a
matter of personal preference. ;, :, and in all _can_ work, and the
differences are primarily cosmetic.

- Jonathan M Davis





More information about the Digitalmars-d mailing list