Explain function syntax

Steven Schveighoffer schveiguy at gmail.com
Sat Sep 20 02:36:47 UTC 2025


On Friday, 19 September 2025 at 16:58:38 UTC, Ali Çehreli wrote:
> On 9/18/25 10:18 PM, Steven Schveighoffer wrote:
> > On Thursday, 18 September 2025 at 18:10:13 UTC, Ali Çehreli
> wrote:
>
> >>     // Not a delegate:
> >>     static assert(is (typeof(twice) == function));
>
> > You are mistaking the is expression for a function test with
> the
> > function pointer type.
>
> I disagree because it is impossible to mistake much from the 
> documentation of the is expression:
>
>   https://dlang.org/spec/expression.html#is_expression

`is(T == function)`

means, is `T` a type, and is it a *function* type. Not a 
*function pointer* type.

What is a function type? It's the internal type that the compiler 
has for a function, which you actually cannot express in syntax.

Note from my example how both functions that need context and 
functions that do not need context both satisfy this test. 
Because they are all functions.

> I stress strongly that there is not enough information there to 
> take anything incorrectly. The reader has to construct a 
> consistent mental model from that documentation. I did and my 
> mental model is intact.
>
> I did test with 'function' against 'delegate' and it worked as 
> I still understand it. For the example I wrote, 'is' expression 
> succeeds for 'function' but not for 'delegate' if the nested 
> function makes use of local scope.

```d
void main()
{
     int x;
     int iNeedContext() {
         return x;
     }
     static int iDontNeedContext() {
         return 5;
     }
     static assert(is(typeof(iNeedContext) == function));
     static assert(is(typeof(iDontNeedContext) == function));
     static assert(!is(typeof(&iNeedContext) == function));
     static assert(!is(typeof(&iDontNeedContext) == function));
}
```

I don't know how to show it any clearer. `is(T == function)` is 
testing whether `T` is a function type, not whether it needs 
context. It is always false for types that are not functions 
(including function pointers or delegates).

-Steve


More information about the Digitalmars-d-learn mailing list