cannot interpret `cast(void)0` at compile time?

Dadoum dadoum at protonmail.com
Wed May 10 05:39:43 UTC 2023


On Tuesday, 9 May 2023 at 23:27:07 UTC, Salih Dincer wrote:
> To tell the truth, I'm a little confused. 😀
>
> So I can't understand why we should use the delegate. Below I 
> will share a similar example and it includes a wrapper (Bar), 
> your class (PlistDict) and it works:
>
>
> ```d
> alias AA = Bar[string];
>
> struct Bar // Wrapper
> {
>   int value;
> }
>
> class Foo // PlistDict
> {
>   void opIndexAssign(Bar element, string key)
>   {
>     import std.stdio;
>     key.writeln(" added, value: ", element.value);
>   }
> }
>
> template PL(alias R) // pl
> {
>   static if(is(typeof(R) == AA))
>   {
>     auto PL()
>     {
>       auto dict = new Foo();
>       static foreach (K, V; R)
>       {
>         dict[K] = V;
>       }
>       return dict;
>     }
>   } else static if(is(typeof(R) == int)) {
>
>     enum PL = Bar(R);
>
>   } else {
>
>     enum PL = Bar();
>
>   }
> }
>
> void main()
> {
>   enum two = 2;
>   auto request = PL!(
>        [
>           "one": PL!1,
>           "two": PL!two,
>          "zero": PL!"zero"
>        ]
>   );
> } /*
> one added, value: 1
> two added, value: 2
> zero added, value: 0
> */
> ```

The problem I encounter happens because one of the variable is a 
run time one. Instead of `enum two`, there is `auto two`, and 
when we pass it in a template, we have to put in a delegate to 
prevent the compiler from evaluating it (it can’t because it’s 
runtime). But then the template has to create a delegate to a 
local variable and the scope where you can use it is very limited.




More information about the Digitalmars-d mailing list