What is "dual-context" ?

Paul Backus snarwin at gmail.com
Fri Sep 13 04:23:47 UTC 2019


On Friday, 13 September 2019 at 02:49:33 UTC, SrMordred wrote:
> source\app.d(37,10): Error: function `app.main.match!((some) => 
> print(some), (none) => print("none")).match` requires a 
> dual-context, which is not yet supported by LDC
>
> I was playing with my home made "sumtypes".
> The code below works fine in dmd, but in ldc it triggers that 
> error.
>
> x.match!(some => print(some), none => print("none"));
>
>
> What exactly is this "dual-context"?

"Dual context" is the compiler feature that allows you to pass 
delegates as template arguments to member functions. For a long 
time, limitations in the frontend made this impossible [1]. It 
was recently fixed in dmd [2], but the fix hasn't made it into 
ldc yet [3], so code that takes advantage of this feature is 
currently dmd-only.

The easiest way to work around the issue is to make `match` a 
non-member function, and call it using UFCS. This is what the dub 
package `sumtype` does [4].

[1] https://issues.dlang.org/show_bug.cgi?id=5710
[2] https://github.com/dlang/dmd/pull/9282
[3] https://github.com/ldc-developers/ldc/issues/3125
[4] 
https://github.com/pbackus/sumtype/blob/v0.8.13/src/sumtype.d#L1091-L1106


More information about the Digitalmars-d-learn mailing list