www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Recursive delegate inside template?

reply drathier <forum.dlang.org fi.fo> writes:
I'm trying to get this to compile, without much luck:

```
bool foo()
{
     template fn(A)
     {
         A delegate(A) fn;
         fn = delegate A(A a)
         {
             return fn(a);
         };
     }

     return fn!(bool)(true);
}
```

What am I missing here? How can I define a recursive delegate 
inside another function like this? I even got a gold linker error 
from run.dlang.io at one point: "TLS relocation against invalid 
instruction".

Removing the template works fine:

```
bool foo()
{
     alias A = bool;
     A delegate(A) fn;
     fn = delegate A(A a) { return fn(a); };

     return fn(true);
}
```
but I need the function to be polymorphic, so I need the template.
Jun 26 2020
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 26 June 2020 at 14:12:00 UTC, drathier wrote:
 I'm trying to get this to compile, without much luck:
You might be able to make it a function: bool foo() { auto fn(A)() { A delegate(A) fn; fn = delegate A(A a) { return fn(a); }; return fn; } return fn!(bool)()(true); }
 but I need the function to be polymorphic, so I need the 
 template.
I don't really understand your need though, can you post more context?
Jun 26 2020
parent drathier <forum.dlang.org fi.fo> writes:
On Friday, 26 June 2020 at 15:10:07 UTC, Adam D. Ruppe wrote:
 On Friday, 26 June 2020 at 14:12:00 UTC, drathier wrote:
 I'm trying to get this to compile, without much luck:
You might be able to make it a function: bool foo() { auto fn(A)() { A delegate(A) fn; fn = delegate A(A a) { return fn(a); }; return fn; } return fn!(bool)()(true); }
I tried this, but it gives me `Error: non-constant expression &fn` which I don't understand. Is ctfe kicking in even though this is inside a function? ``` void foo2() { auto fn(A) = std.functional.toDelegate(({ A fn(A)(A a) { return a; } return &fn!(A); }())); } ```
 but I need the function to be polymorphic, so I need the 
 template.
I don't really understand your need though, can you post more context?
I'm generating code, compiling a strict pure functional language to D. The code that I'm trying to compile is this example function: ``` fn a = let _ = fn a in a ``` which gets the type `forall a. a -> a` inferred. That's something like ``` A delegate(A) fn(A) = delegate A (A a) { auto _ = fn!(A)(a); return a; }; ``` in D syntax, if it would compile. Well, it compiles, but the linker fails `error: TLS relocation against invalid instruction`. It's a simplified example that still tests these features: - recursion - polymorphism - functions as values (closures/delegates)
Jun 26 2020
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Jun 26, 2020 at 02:12:00PM +0000, drathier via Digitalmars-d-learn
wrote:
 I'm trying to get this to compile, without much luck:
 
 ```
 bool foo()
 {
     template fn(A)
     {
         A delegate(A) fn;
         fn = delegate A(A a)
         {
             return fn(a);
         };
     }
 
     return fn!(bool)(true);
 }
 ```
 
 What am I missing here? How can I define a recursive delegate inside
 another function like this?
[...] Sounds like you're looking for the equivalent of the Y combinator, perhaps? https://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator T -- Never trust an operating system you don't have source for! -- Martin Schulze
Jun 26 2020