digitalmars.D.learn - Associative Array, get value instead of poiter using `if (auto ..)`,
- ryuukk_ (12/12) Aug 31 ```D
- Steven Schveighoffer (3/15) Aug 31 Maybe if(auto it = test.get(“hello”, 0))
- ryuukk_ (3/22) Aug 31 Now i can't use 0 as a value, that's not a solution
- kdevel (3/27) Sep 01 Shall the if clause evaluate as true if the value of the AA
- Paul Backus (23/35) Aug 31 Once the next release of Phobos comes out, with [PR 9039][1]
- ryuukk_ (5/44) Aug 31 See, that's the thing i hate about D, solving simple problem
- ryuukk_ (14/14) Aug 31 Let's see how other languages do it:
- Lance Bachmeier (2/16) Aug 31 It's good you're not in charge here because that is hideous.
- ryuukk_ (5/26) Aug 31 Paul Backup solution is infinity better, i apologies
- Richard (Rikki) Andrew Cattermole (3/25) Aug 31 Hey now, there is no need to get personal, ryuukk is evaluating out
- Nick Treleaven (16/29) Sep 02 We don't have `if` callback syntax, but essentially you can do
- Nick Treleaven (2/4) Sep 02 https://issues.dlang.org/show_bug.cgi?id=24255
- Nick Treleaven (4/7) Sep 02 Sorry, that aborts if the key isn't present. `update` requires
- kdevel (14/36) Aug 31 Is that functionally different from
- Paul Backus (7/19) Aug 31 It's essentially the same. I only suggested it because the
```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)
Aug 31
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 = test.get(“hello”, 0)) -Steve
Aug 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:Now i can't use 0 as a value, that's not a solution```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 = test.get(“hello”, 0)) -Steve
Aug 31
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:Shall the if clause evaluate as true if the value of the AA lookup coerces to false (and vice versa)?On Saturday, 31 August 2024 at 12:47:25 UTC, ryuukk_ wrote:Now i can't use 0 as a value, that's not a solution```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 = test.get(“hello”, 0)) -Steve
Sep 01
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 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 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); } } ``` [1]: https://github.com/dlang/phobos/pull/9039
Aug 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: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```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 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 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); } } ``` [1]: https://github.com/dlang/phobos/pull/9039
Aug 31
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
Aug 31
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 phobosIt's good you're not in charge here because that is hideous.
Aug 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: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 betterLet'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 phobosIt's good you're not in charge here because that is hideous.
Aug 31
On 01/09/2024 4:34 AM, Lance Bachmeier wrote:On Saturday, 31 August 2024 at 15:38:49 UTC, ryuukk_ wrote:Hey now, there is no need to get personal, ryuukk is evaluating out ideas and there is nothing wrong with this.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 phobosIt's good you're not in charge here because that is hideous.
Aug 31
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 LINERWe 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.
Sep 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
Sep 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.
Sep 02
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 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); } } ``` ?
Aug 31
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. :)
Aug 31
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:you are wrong, the question wasn't about "an alternative to pointer" i'll quote myself: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.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 longerI 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
Sep 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 DAn 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: ```d // 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); }); ```
Sep 01