What does the [] operator do here?

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 1 19:45:53 UTC 2020


On 4/1/20 3:35 PM, Net wrote:
> from the below code, the expression "case [c]":
> 
> void main()
> {
>      import std.stdio, std.string, std.algorithm, std.conv;
> 
>      // Reduce the RPN expression using a stack
>      readln.split.fold!((stack, op)
>      {
>          switch (op)
>          {
>              // Generate operator switch cases statically
>              static foreach (c; "+-*/")
>                  case [c]:
>                      return stack[0 .. $ - 2] ~
>                          mixin("stack[$ - 2] " ~ c ~
>                              " stack[$ - 1]");
>              default: return stack ~ op.to!real;
>          }
>      })((real[]).init).writeln;
> }
> 

heh, it's an array of one character, i.e. an immutable char[] of length 
1 (or a string).

Kind of clever actually.

-Steve


More information about the Digitalmars-d-learn mailing list