Jump to page: 1 2
Thread overview
Is it possible to use tokens as template parameters?
Aug 14, 2021
rempas
Aug 14, 2021
Tejas
Aug 14, 2021
rempas
Aug 14, 2021
evilrat
Aug 14, 2021
Alexandru Ermicioi
Aug 15, 2021
rempas
Aug 14, 2021
jfondren
Aug 15, 2021
rempas
Aug 15, 2021
Daniel N
Aug 16, 2021
Ali Çehreli
Aug 16, 2021
Ali Çehreli
August 14, 2021

Is it possible to do something like that?

mixin template lel(alias N, alias V) {
  auto N = V;
}

void main() {
  mixin lel!(name, "Hmmmm");
}

In this case, it would (if it was possible) get replaced with: auto name = "Hmmmm";
Is there something I'm missing?

August 14, 2021

On Saturday, 14 August 2021 at 11:23:12 UTC, rempas wrote:

>

Is it possible to do something like that?

mixin template lel(alias N, alias V) {
  auto N = V;
}

void main() {
  mixin lel!(name, "Hmmmm");
}

In this case, it would (if it was possible) get replaced with: auto name = "Hmmmm";
Is there something I'm missing?

You have to use mixins to achieve the desired effect.

mixin template lel(T, string N, string V) {
    mixin(T.stringof ~ " " ~N ~ " = "  ~ V ~ ";");
}

import std;
void main(){
    mixin lel!(string, "name", ` "hmmmm" `);
    writeln(name);
}
}
August 14, 2021

On Saturday, 14 August 2021 at 12:19:59 UTC, Tejas wrote:

>

On Saturday, 14 August 2021 at 11:23:12 UTC, rempas wrote:

>

Is it possible to do something like that?

mixin template lel(alias N, alias V) {
  auto N = V;
}

void main() {
  mixin lel!(name, "Hmmmm");
}

In this case, it would (if it was possible) get replaced with: auto name = "Hmmmm";
Is there something I'm missing?

You have to use mixins to achieve the desired effect.

mixin template lel(T, string N, string V) {
    mixin(T.stringof ~ " " ~N ~ " = "  ~ V ~ ";");
}

import std;
void main(){
    mixin lel!(string, "name", ` "hmmmm" `);
    writeln(name);
}
}

The behavior I want to achieve is not having to enclose the word (tokens) in double quotes.

August 14, 2021

On Saturday, 14 August 2021 at 13:50:52 UTC, rempas wrote:

>

The behavior I want to achieve is not having to enclose the word (tokens) in double quotes.

Then it is not possible. D is strictly hygienic in that regard. You either use explicit mixin or string paramter.

It is possible to have it with alias by using std.meta Alias!"name_here" or similar short template like below, but both has same limitation, there is absolutely no way to get rid of quotes.

 auto t(T) {
   enum t = T;
 }

 writeln("token".t);
August 14, 2021

On Saturday, 14 August 2021 at 13:50:52 UTC, rempas wrote:

>

The behavior I want to achieve is not having to enclose the word (tokens) in double quotes.

The closest you can get is using q{mixed d code} syntax that is basically a string containing d code, and hence subject to some checkups, and ide highlighting.

Though why do you need this?

Regards,
Alexandru

August 14, 2021

On Saturday, 14 August 2021 at 11:23:12 UTC, rempas wrote:

>

Is it possible to do something like that?

mixin template lel(alias N, alias V) {
  auto N = V;
}

void main() {
  mixin lel!(name, "Hmmmm");
}

In this case, it would (if it was possible) get replaced with: auto name = "Hmmmm";
Is there something I'm missing?

When I was learning Rust I kept running into syntaxes like this in books and examples. The funny thing was that the latest versions of the libraries had all gotten rid of these macros. To be able to put something like mime!(text/html) in your code seemed cool, using Rust's token-based macros, but then it was more trouble than it was worth and the fad ended with prejudice.

Your exact usage can be made to work:

enum prop { name, title, location, etc }
enum { name, title, location, etc }

mixin template lel(alias p, alias v) {
    import std.traits : hasMember;

    static assert(hasMember!(prop, p.stringof));
    mixin("auto " ~ p.stringof ~ " = " ~ v.stringof ~ ";");
}

void main() {
    import std.stdio : writeln;

    mixin lel!(name, "Hmmmm");
    writeln(name);
}

And how might this be more trouble than it's worth?

  1. name is always a valid identifier prior to its definition, so rearranging your code can result in surprising behavior (writeln(name) // oops this is 0) without an error, not even a complaint about shadowing.
  2. v is more limited in expression than you might think. For example, this fails:
auto x = 2;
mixin lel!(name, &x);  // variable `x` cannot be read at compile time
x++;
writeln(*name);        // can only `*` a pointer, not a `int`
August 15, 2021

On Saturday, 14 August 2021 at 15:21:51 UTC, Alexandru Ermicioi wrote:

>

On Saturday, 14 August 2021 at 13:50:52 UTC, rempas wrote:

>

The behavior I want to achieve is not having to enclose the word (tokens) in double quotes.

The closest you can get is using q{mixed d code} syntax that is basically a string containing d code, and hence subject to some checkups, and ide highlighting.

Though why do you need this?

Regards,
Alexandru

I mean I don't... But I was just curious if that's possible. Using a constant would probably also work I guess.

August 15, 2021

On Saturday, 14 August 2021 at 20:35:18 UTC, jfondren wrote:

>

On Saturday, 14 August 2021 at 11:23:12 UTC, rempas wrote:

>

[...]

When I was learning Rust I kept running into syntaxes like this in books and examples. The funny thing was that the latest versions of the libraries had all gotten rid of these macros. To be able to put something like mime!(text/html) in your code seemed cool, using Rust's token-based macros, but then it was more trouble than it was worth and the fad ended with prejudice.

[...]

Makes sense. I also thought that Rust's macro_rules! were cool but I understand your point. Thanks a lot!

August 15, 2021

On Sunday, 15 August 2021 at 05:47:34 UTC, rempas wrote:

> >

[...]

Makes sense. I also thought that Rust's macro_rules! were cool but I understand your point. Thanks a lot!

opDispatch can create strings, which can either be directly mixed in, or the generated string can be send to mixin templates.

import std;

static struct Yay
{
    string opDispatch(string name, T...)(T vals)
    {
        return q{
           auto %s = 777;
        }.format(name);
    }
}
enum yay=Yay();

void main()
{
    mixin(yay.magic);

    magic.writeln;
}
August 16, 2021
On 8/15/21 1:40 AM, Daniel N wrote:

> static struct Yay
> {
>      string opDispatch(string name, T...)(T vals)

I sometimes wish templates had opDispatch abilities. Then we could instantiate a template with a symbol and the the template could receive it as string just like the way it works for opDispatch. So, this could work:

  Flag!isCool isCool;

instead of today's

  Flag!"isCool" isCool;

Basically, it would still be a string parameter but the quotes need not be provided:

template Flag(string tag) {
  // ...
}

I realize that this might hide some bugs but so does opDispatch, no?

Ali

« First   ‹ Prev
1 2