Thread overview
simple question about UFCS and templates...wasFound(i) works but not i.wasFound()
Mar 19
monkyyy
Mar 21
Dennis
March 19
   bool wasFound(I)(I result)
    {
        return(result != -1);
    }

    bool existPoint(Point b, int cost)
    {
        auto i = closed.countUntil(b);

        if(wasFound(i))   // -1 is returned if no point b is found in the range

The above compiles and executes successfully. But the following fails with:
app.d(179,13): Error: no property wasFound for i of type long

I thought the templated function would take care of any type.

    bool wasFound(I)(I result)
    {
        return(result != -1);
    }

    bool existPoint(Point b, int cost)
    {
        auto i = closed.countUntil(b);  // Note: closed

        if(i.wasFound())   // -1 is returned if no point b is found in the range
March 19
On Wed, Mar 19, 2025 at 10:26:54PM +0000, WhatMeWorry via Digitalmars-d-learn wrote: [...]
> The above compiles and executes successfully. But the following fails with: app.d(179,13): Error: no property `wasFound` for `i` of type `long`
> 
> I thought the templated function would take care of any type.
> ```
>     bool wasFound(I)(I result)
>     {
>         return(result != -1);
>     }
> 
>     bool existPoint(Point b, int cost)
>     {
>         auto i = closed.countUntil(b);  // Note: closed
> 
>         if(i.wasFound())   // -1 is returned if no point b is found in the range
> ```

Where is wasFound declared?  If you want UFCS syntax to work, wasFound must be declared in global (module) scope. Otherwise, it will not be considered for UFCS when resolving identifiers.


T

-- 
The cat owns the house; that's why the word "homeowner" has "meow" in it.
March 19

On Wednesday, 19 March 2025 at 22:26:54 UTC, WhatMeWorry wrote:

>
   bool wasFound(I)(I result)
    {
        return(result != -1);
    }

    bool existPoint(Point b, int cost)
    {
        auto i = closed.countUntil(b);

        if(wasFound(i))   // -1 is returned if no point b is found in the range

The above compiles and executes successfully. But the following fails with:
app.d(179,13): Error: no property wasFound for i of type long

I thought the templated function would take care of any type.

    bool wasFound(I)(I result)
    {
        return(result != -1);
    }

    bool existPoint(Point b, int cost)
    {
        auto i = closed.countUntil(b);  // Note: closed

        if(i.wasFound())   // -1 is returned if no point b is found in the range

ufcs on local functions is hit or miss; theres a old limitation that d1 wanted or something; ussally templates do fix it but without full context its hard for me to tell the exact reason.

March 19
On Wed, Mar 19, 2025 at 11:21:15PM +0000, monkyyy via Digitalmars-d-learn wrote: [...]
> ufcs on local functions is hit or miss;
[...]

I thought it was always a miss. :-D  At least, it's never worked for me every time I tried it.  I always have to move the UFCS function to module scope, then it works.


T

-- 
What's an anagram of "BANACH-TARSKI"?  BANACH-TARSKI BANACH-TARSKI.
March 19
On Wednesday, March 19, 2025 5:48:37 PM MDT H. S. Teoh via Digitalmars-d-learn wrote:
> On Wed, Mar 19, 2025 at 11:21:15PM +0000, monkyyy via Digitalmars-d-learn wrote: [...]
> > ufcs on local functions is hit or miss;
> [...]
>
> I thought it was always a miss. :-D  At least, it's never worked for me every time I tried it.  I always have to move the UFCS function to module scope, then it works.

AFAIK, it's always a miss. There's a bug report somewhere where Kenji explained exactly why it works the way that it does, but I don't recall what he said. I also don't know if it was closed as "won't fix" or remember much that would make it easy to find, unfortunately. :|

But there is a technical reason for the limitation, even if I don't remember what it is.

- Jonathan M Davis



March 20
On Thursday, 20 March 2025 at 02:01:25 UTC, Jonathan M Davis wrote:
> On Wednesday, March 19, 2025 5:48:37 PM MDT H. S. Teoh via Digitalmars-d-learn wrote:
>> I thought it was always a miss. :-D  At least, it's never worked for me every time I tried it.  I always have to move the UFCS function to module scope, then it works.
>
> AFAIK, it's always a miss. There's a bug report somewhere where Kenji explained exactly why it works the way that it does, but I don't recall what he said. I also don't know if it was closed as "won't fix" or remember much that would make it easy to find, unfortunately. :|
>
> But there is a technical reason for the limitation, even if I don't remember what it is.

It's to prevent UFCS functions from being shadowed by local variables. From the spec:

> Rationale: Local function symbols are not considered by UFCS to avoid unexpected name conflicts.

https://dlang.org/spec/function.html#pseudo-member
March 20
Yup. That was the problem. Thank you. You guys are a sharp.
March 21

On Thursday, 20 March 2025 at 16:18:32 UTC, WhatMeWorry wrote:

>

Yup. That was the problem. Thank you. You guys are a sharp.

From dmd version 2.111, there will be a better error message in this case.

https://github.com/dlang/dmd/pull/21046

March 21

On Friday, 21 March 2025 at 13:41:18 UTC, Dennis wrote:

>

On Thursday, 20 March 2025 at 16:18:32 UTC, WhatMeWorry wrote:

>

Yup. That was the problem. Thank you. You guys are a sharp.

From dmd version 2.111, there will be a better error message in this case.

https://github.com/dlang/dmd/pull/21046

THANKS. I LOVE D!