Recursive delegate inside template?
drathier
forum.dlang.org at fi.fo
Fri Jun 26 16:28:44 UTC 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'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)
More information about the Digitalmars-d-learn
mailing list