digitalmars.D.learn - Recursive delegate inside template?
- drathier (30/30) Jun 26 2020 I'm trying to get this to compile, without much luck:
- Adam D. Ruppe (17/20) Jun 26 2020 You might be able to make it a function:
- drathier (40/60) Jun 26 2020 I tried this, but it gives me `Error: non-constant expression
- H. S. Teoh (8/28) Jun 26 2020 [...]
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
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
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 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); }())); } ```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'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)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
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