Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread Paul Backus via Digitalmars-d-learn
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

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
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    

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread kdevel via Digitalmars-d-learn
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: ```d import std.typecons; Nullable!V maybeGet(K, V)(V[K] aa, K key) { if (auto ptr = key in aa) return null

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread ryuukk_ via Digitalmars-d-learn
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: ```zig map.put("hello", 42); // get pointer if (map.get("hello")) |*it| { std.log.debug("{}", .{it}); }

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread Lance Bachmeier via Digitalmars-d-learn
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.deb

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread ryuukk_ via Digitalmars-d-learn
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 LIN

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread ryuukk_ via Digitalmars-d-learn
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: ```D 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 point

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 31 August 2024 at 12:47:25 UTC, ryuukk_ wrote: ```D 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 rel

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread ryuukk_ via Digitalmars-d-learn
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: ```D void main() { int[string] test; test["hello"] = 42; if (auto it = "hello" in test) { } } ``` Is there a way to get the value instead o

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On Saturday, 31 August 2024 at 12:47:25 UTC, ryuukk_ wrote: ```D 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

Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-08-31 Thread ryuukk_ via Digitalmars-d-learn
```D 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)