Re: Pure Factory Functions 💔 `inout`

Steven Schveighoffer schveiguy at gmail.com
Fri Feb 10 14:00:20 UTC 2023


On 2/10/23 7:53 AM, Kagamin wrote:
> On Thursday, 9 February 2023 at 18:47:17 UTC, Steven Schveighoffer wrote:
>> a `pure` function which takes an `inout` reference and returns a 
>> `mutable` reference should always be implicitly convertible to 
>> anything you want. It's no different from `const` in this regard.
> 
> That's a weakly pure function, so its return value can convert only to 
> const. Maybe it can convert to inout too, but the language doesn't 
> recognize this pattern.

It can be weakly pure. The docs are wrong.

i.e. this works:

```d
pure int *foo(const int *p)
{
    return new int(5);
}

void main()
{
    int x;
    immutable int * bar = foo(&x);
}
```

-Steve


More information about the Digitalmars-d mailing list