February 19, 2023
On Sunday, 19 February 2023 at 18:08:28 UTC, Steven Schveighoffer wrote:
> On 2/18/23 2:25 PM, Nick Treleaven wrote:
>> That's it, nothing about why isInputRange failed. Maybe I'm doing something wrong.
>
> uhh... I think I am wrong on the reasoning then. I thought it would keep going down into the constraint, but it doesn't? Maybe it was planned?

OK, it could be implemented. Perhaps with complex failing constraints it would be too much information if it recursed into each template predicate too (though could be done with `-v`).

>> (aside from the inout issue):
>> ```d
>> template isInputRange(R) {
>>      extern R r; // dummy
>>      enum isInputRange =
>>          is(typeof(R.init) == R) &&
>>          is(typeof({ return r.empty; }()) == bool) &&
>>          (is(typeof(() return => r.front)) ||
>>              is(typeof(ref () return => r.front))) &&
>>          !is(typeof({ return r.front; }()) == void) &&
>>          is(typeof({ r.popFront; }));
>> }
>> ```
>> dmd could still see through the eponymous template to the expression, in theory.
>
> I don't know if that works, would it fail to link? It's an interesting idea.

It doesn't cause a link error on my Linux machine, probably the linker detects it's not actually used anywhere.

But declaring an `extern` variable of a type that contains `inout` data is an error:
```
isinputrange.d(54): Error: variable `isinputrange.isInputRange!(inout(int)[]).r` only parameters or stack based variables can be `inout`
```
So this pattern won't work.

>>> I actually did get a PR merged. It wasn't as simple as I had written in that blog post, due to the stupid `inout` requirement that `inout` data can only be used inside a function with an `inout` parameter.
>> 
>> OK, so `typeof((R r) { return r.empty; } (R.init))` works with inout.
>
> Yes, that's what I found that worked. You need the lambda parameter to mimic the mutability of the type. Which kind of sucks, but it's what we have right now.

Would be nice if that rule didn't apply when the `inout` data is a member of another type you're instantiating.

>> Thanks for the blog post BTW.
>
> You're welcome! I write blog posts too infrequently, but I do have a bunch of half-started ones. I'm working on another right now...

Great!

1 2 3
Next ›   Last »