Wishlist proposal, documenting fn arg as in or out or mod
GrimMaple
grimmaple95 at gmail.com
Tue Jun 20 20:44:42 UTC 2023
On Tuesday, 20 June 2023 at 16:18:16 UTC, Dom DiSc wrote:
> This is what "ref" is for.
No, ref is to not pass arguments on stack. Though having three
ways to do so isn't helping with clarifying what the function
will actually do :)
> Hurray!
> I'm so glad this is not allowed. "in" means const. There is no
> gain in "const const".
Only in D spec. `in` doesn't have to mean `const`, the language
can be designed in a way to allow something like this:
```d
void fn(in T1 param1, in out T2 param2);
```
> Sorry, but if a function takes a "ref" that it never modifies,
> I would call this simply a bug. It prevents you from calling it
> with const or immutable arguments while this would be perfectly
> valid.
> So "ref" is documenting that the function will modify this
> parameter. You can never expect anything else (or maybe curse
> the stupid developer that missed marking the parameter as
> const).
Again, only in D spec. However, if you want to be pedantic,
`const ref Type` makes 0 sense, because all references are
`const` already (you can't change the _reference_, you can change
the _referenced object_). What you probably want to mean is `ref
const(Type)` instead, making it a const reference to a const
`Type`.
I understand the idea behind using `in`, `out`, and `inout` to
better explain what parameters actually do, but D's support for
that is very poor. Yet, somehow, very complicated at the same
time.
Fun fact: this is compileable D code
```d
void foo(out int a)
{
writeln(a);
}
```
When it should error out with `"a" isn't assigned a value`.
Instead, it defaults to `init` :)
More information about the Digitalmars-d
mailing list