October 28

On Saturday, 28 October 2023 at 13:13:16 UTC, Paul Backus wrote:

>

On Thursday, 26 October 2023 at 11:18:46 UTC, kdevel wrote:

>

Accessing Elements Of An AA

string[string] aa;
aa["name"] = "value";
writeln (i"The name in s is $(aa[\"name\"])"

That typing is laborious. Isn't there a way to bind the expression to the keys of the AA?

You can do this with existing language features: https://run.dlang.io/is/QRFNpg

Although I wouldn't really recommend it, since it forces you to write fully-qualified names to access anything that isn't an associative-array key.

https://run.dlang.io/is/jyNmOi
different solution

November 01

On Saturday, 28 October 2023 at 13:13:16 UTC, Paul Backus wrote:

> >

[...]
That typing is laborious. Isn't there a way to bind the expression to the keys of the AA?

You can do this with existing language features: https://run.dlang.io/is/QRFNpg

Amazing!

>

Although I wouldn't really recommend it, since it forces you to write fully-qualified names to access anything that isn't an associative-array key.

The simple name (x) seems to work: https://run.dlang.io/is/Nzne84

import std.stdio;

void main()
{
    string[string] aa;
    string x = "xxx";
    aa["name"] = "Arthur";
    aa["quest"] = "seek the Holy Grail";
    aa["favoriteColor"] = "blue";

    with (aa.keysAsVars)
    .writefln!"My name is %s, I %s, and my favorite color is %s."(
    	name, quest, x);
}

struct KeysAsVars(K, V)
{
    V[K] aa;
    V opDispatch(string key)() => aa[key];
}

KeysAsVars!(K, V) keysAsVars(K, V)(V[K] aa)
{
    return typeof(return)(aa);
}
November 02

On Thursday, 26 October 2023 at 11:18:46 UTC, kdevel wrote:

>

Accessing Elements Of An AA

string[string] aa;
aa["name"] = "value";
writeln (i"The name in s is $(aa[\"name\"])"

I always thought string interpolation is literally some basic syntax sugar, like:

string s = "$foo ${bar+1}";

would be lowered to:

string s = foo.to!string ~ " " ~ (bar+1).to!string;

I guess it would get problematic when people want to use strings other than the built-in one though.

November 02

On Wednesday, 1 November 2023 at 22:22:42 UTC, kdevel wrote:

>

On Saturday, 28 October 2023 at 13:13:16 UTC, Paul Backus wrote:

>

Although I wouldn't really recommend it, since it forces you to write fully-qualified names to access anything that isn't an associative-array key.

The simple name (x) seems to work: https://run.dlang.io/is/Nzne84

Interesting. It seems like symbols in the current module take precedence over opDispatch, but symbols in other modules don't. I wonder if that's intentional?

1 2
Next ›   Last »