Jump to page: 1 2
Thread overview
Associative Array, get value instead of poiter using `if (auto ..)`, possible?
Aug 31
ryuukk_
Aug 31
ryuukk_
Sep 01
kdevel
Aug 31
ryuukk_
Aug 31
ryuukk_
Aug 31
ryuukk_
Aug 31
kdevel
Sep 01
ryuukk_
Sep 01
evilrat
August 31
void main()
{
    int[string] test;

    test["hello"] = 42;

    if (auto it = "hello" in test)
    {

    }

}

Is there a way to get the value instead of a pointer? while keeping the conciseness (one line)

August 31

On Saturday, 31 August 2024 at 12:47:25 UTC, ryuukk_ wrote:

>
void main()
{
    int[string] test;

    test["hello"] = 42;

    if (auto it = "hello" in test)
    {

    }

}

Is there a way to get the value instead of a pointer? while keeping the conciseness (one line)

Maybe if(auto it = test.get(“hello”, 0))

-Steve

August 31

On Saturday, 31 August 2024 at 13:00:42 UTC, Steven Schveighoffer wrote:

>

On Saturday, 31 August 2024 at 12:47:25 UTC, ryuukk_ wrote:

>
void main()
{
    int[string] test;

    test["hello"] = 42;

    if (auto it = "hello" in test)
    {

    }

}

Is there a way to get the value instead of a pointer? while keeping the conciseness (one line)

Maybe if(auto it = test.get(“hello”, 0))

-Steve

Now i can't use 0 as a value, that's not a solution

August 31

On Saturday, 31 August 2024 at 12:47:25 UTC, ryuukk_ wrote:

>
void main()
{
    int[string] test;

    test["hello"] = 42;

    if (auto it = "hello" in test)
    {

    }

}

Is there a way to get the value instead of a pointer? while keeping the conciseness (one line)

Once the next release of Phobos comes out, with PR 9039 merged, you'll be able to do it like this:

import std.typecons;

Nullable!V maybeGet(K, V)(V[K] aa, K key)
{
    if (auto ptr = key in aa)
        return nullable(*ptr);
    else
        return Nullable!V.init;
}

void main()
{
    import std.stdio;

    int[string] test = ["hello": 42];

    if (auto it = test.maybeGet("hello"))
    {
        writeln("hello => ", it.get);
    }
}
August 31

On Saturday, 31 August 2024 at 14:25:29 UTC, Paul Backus wrote:

>

On Saturday, 31 August 2024 at 12:47:25 UTC, ryuukk_ wrote:

>
void main()
{
    int[string] test;

    test["hello"] = 42;

    if (auto it = "hello" in test)
    {

    }

}

Is there a way to get the value instead of a pointer? while keeping the conciseness (one line)

Once the next release of Phobos comes out, with PR 9039 merged, you'll be able to do it like this:

import std.typecons;

Nullable!V maybeGet(K, V)(V[K] aa, K key)
{
    if (auto ptr = key in aa)
        return nullable(*ptr);
    else
        return Nullable!V.init;
}

void main()
{
    import std.stdio;

    int[string] test = ["hello": 42];

    if (auto it = test.maybeGet("hello"))
    {
        writeln("hello => ", it.get);
    }
}

See, that's the thing i hate about D, solving simple problem requires templates + import + function call

I already lost interest, i'll solve that problem with something else

August 31

Let's see how other languages do it:

    map.put("hello", 42);

    // get pointer
    if (map.get("hello")) |*it| {
        std.log.debug("{}", .{it});
    }

    // get value
    if (map.get("hello")) |it| {
        std.log.debug("{}", .{it});
    }

No imports, no templates, ONE LINER

Please submit PRs to DMD, not phobos

August 31

On Saturday, 31 August 2024 at 15:38:49 UTC, ryuukk_ wrote:

>

Let's see how other languages do it:

    map.put("hello", 42);

    // get pointer
    if (map.get("hello")) |*it| {
        std.log.debug("{}", .{it});
    }

    // get value
    if (map.get("hello")) |it| {
        std.log.debug("{}", .{it});
    }

No imports, no templates, ONE LINER

Please submit PRs to DMD, not phobos

It's good you're not in charge here because that is hideous.

August 31

On Saturday, 31 August 2024 at 16:34:00 UTC, Lance Bachmeier wrote:

>

On Saturday, 31 August 2024 at 15:38:49 UTC, ryuukk_ wrote:

>

Let's see how other languages do it:

    map.put("hello", 42);

    // get pointer
    if (map.get("hello")) |*it| {
        std.log.debug("{}", .{it});
    }

    // get value
    if (map.get("hello")) |it| {
        std.log.debug("{}", .{it});
    }

No imports, no templates, ONE LINER

Please submit PRs to DMD, not phobos

It's good you're not in charge here because that is hideous.

Paul Backup solution is infinity better, i apologies

I'll now leave, just like everyone else, sorry for the noise, who am i to expect improvements, i should have known better

August 31

On Saturday, 31 August 2024 at 14:25:29 UTC, Paul Backus wrote:

>

[...]
Once the next release of Phobos comes out, with [PR 9039][1] merged, you'll be able to do it like this:

import std.typecons;

Nullable!V maybeGet(K, V)(V[K] aa, K key)
{
    if (auto ptr = key in aa)
        return nullable(*ptr);
    else
        return Nullable!V.init;
}

void main()
{
    import std.stdio;

    int[string] test = ["hello": 42];

    if (auto it = test.maybeGet("hello"))
    {
        writeln("hello => ", it.get);
    }
}

Is that functionally different from

void main()
{
    import std.stdio;

    int[string] test = ["hello": 42];

    if (auto p = "hello" in test)
    {
        writeln("hello => ", *p);
    }
}

?

September 01
On 01/09/2024 4:34 AM, Lance Bachmeier wrote:
> On Saturday, 31 August 2024 at 15:38:49 UTC, ryuukk_ wrote:
>> Let's see how other languages do it:
>>
>> ```zig
>>     map.put("hello", 42);
>>
>>     // get pointer
>>     if (map.get("hello")) |*it| {
>>         std.log.debug("{}", .{it});
>>     }
>>
>>     // get value
>>     if (map.get("hello")) |it| {
>>         std.log.debug("{}", .{it});
>>     }
>> ```
>>
>> No imports, no templates, ONE LINER
>>
>> Please submit PRs to DMD, not phobos
> 
> It's good you're not in charge here because that is hideous.

Hey now, there is no need to get personal, ryuukk is evaluating out ideas and there is nothing wrong with this.
« First   ‹ Prev
1 2