Associative Array, get value instead of poiter using `if (auto ..)`, possible?
Nick Treleaven
nick at geany.org
Mon Sep 2 11:56:10 UTC 2024
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
We don't have `if` callback syntax, but essentially you can do
that:
```d
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.
More information about the Digitalmars-d-learn
mailing list