September 01

On Saturday, 31 August 2024 at 22:06:26 UTC, kdevel wrote:

>

Is that functionally different from

void main()
{
    import std.stdio;

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

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

It's essentially the same. I only suggested it because the original question was about alternatives to pointers, and Nullable isn't a pointer.

I suppose it goes to show that avoiding language features just for the sake of it ("no pointers", "no templates", "no imports", ...) is unlikely to accomplish anything useful. :)

September 01

On Sunday, 1 September 2024 at 03:06:53 UTC, Paul Backus wrote:

>

On Saturday, 31 August 2024 at 22:06:26 UTC, kdevel wrote:

>

Is that functionally different from

void main()
{
    import std.stdio;

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

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

It's essentially the same. I only suggested it because the original question was about alternatives to pointers, and Nullable isn't a pointer.

you are wrong, the question wasn't about "an alternative to pointer"

i'll quote myself:

>

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

this is not asking for an "alternative"

if i have to call .get or dereference the pointer myself, the premise of "conciseness and one liner" is lost

i pasted a simple example so everyone understands the task, my actual use case is more complex and longer

>

I suppose it goes to show that avoiding language features just for the sake of it ("no pointers", "no templates", "no imports", ...) is unlikely to accomplish anything useful. :)

you didn't understand the question, so why assume this?

if checking for/getting a value from a hashmap requires all that crap, then perhaps something is wrong with the language, and it perhaps isn't the one i should have picked for the task

my mistake perhaps, not yours

besides, i do use the language features in my project, templates and mixins to solve larger problems, it's great, and reason why i picked D

another thing people don't understand:

$ time make build-game

real    0m0.402s
user    0m0.352s
sys     0m0.050s

i value this too, more than anything else, and i like good error messages

i don't like cryptic error messages and 10+ seconds build times

i won't bother reading this thread, as i said, i lost interest, if you can't understand what an improvement is from a user PoV, then there is nothing else for me to say

September 01

On Saturday, 31 August 2024 at 13:48:52 UTC, ryuukk_ wrote:

>

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

Shall the if clause evaluate as true if the value of the AA lookup coerces to false (and vice versa)?

September 01

On Sunday, 1 September 2024 at 08:50:53 UTC, ryuukk_ wrote:

>

if checking for/getting a value from a hashmap requires all that crap, then perhaps something is wrong with the language, and it perhaps isn't the one i should have picked for the task

my mistake perhaps, not yours

besides, i do use the language features in my project, templates and mixins to solve larger problems, it's great, and reason why i picked D

An alternative would be yet another template wrapper for get value that takes a dictionary an a lambda and call that lambda only if value is present, this means you no longer do the if, but rather a "cryptic" call like in some other languages.

pseudo code:

// helper function
void withKey(V,K,L)(V[K] dict, K key, lazy L dostuff) {
    if (auto value = dict.get(K)) {
        doStuff(*value);
    }
}

// usage

string[int] counters;
counters["foo"] = 42;
counters.withKey("foo", (value) {
    assert(value == 42);
});
September 02

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

We don't have if callback syntax, but essentially you can do that:

void main()
{
    int[string] test;

    test["hello"] = 42;
    test.update("hello", () => noreturn.init, (ref int x) {
        x++;
    });
    assert(test["hello"] == 43);
}

Omit the ref if you want a copy of the value. Annoyingly, the int is required, though maybe just an IFTI bug.

September 02

On Monday, 2 September 2024 at 11:56:10 UTC, Nick Treleaven wrote:

>

Annoyingly, the int is required, though maybe just an IFTI bug.

https://issues.dlang.org/show_bug.cgi?id=24255

September 02

On Monday, 2 September 2024 at 11:56:10 UTC, Nick Treleaven wrote:

>
test.update("hello", () => noreturn.init, (ref int x) {
    x++;
});

Sorry, that aborts if the key isn't present. update requires the first callback to provide a value for the key, and it can't return void.

1 2
Next ›   Last »