Emulation macros and pattern matching on D

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 5 06:13:15 PDT 2015


On 6/5/15 9:01 AM, Dennis Ritchie wrote:
> Macros and operation of pattern matching `=>` in Rust I can write
> something like this:
>
> macro_rules!foo {
>      (x => $e:expr) => (println!("mode X: {}", $e));
>      (y => $e:expr) => (println!("mode Y: {}", $e));
> }
>
> fn main() {
>      foo!(y => 3); // mode Y: 3
>      foo!(x => 2); // mode X: 2
> }
>
> How is it possible to simulate in D?


string foo(string mode, string value)
{
    return `writefln("mode ` ~ mode ~ `: %s", ` ~ value ~ `);`;
}

void main()
{
    mixin(foo("Y", "3"));
    mixin(foo("X", "2"));
}

-Steve


More information about the Digitalmars-d-learn mailing list