www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative Array, get value instead of poiter using `if (auto ..)`,

reply ryuukk_ <ryuukk.dev gmail.com> writes:
```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
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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 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
Aug 31
parent kdevel <kdevel vogtner.de> writes:
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:
 ```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
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)?
Sep 01
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
next sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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 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
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
Aug 31
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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
next sibling parent reply Lance Bachmeier <no spam.net> writes:
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.
Aug 31
next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
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});
     }

     // 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
Aug 31
prev sibling parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
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.
Aug 31
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
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.
Sep 02
next sibling parent Nick Treleaven <nick geany.org> writes:
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
prev sibling parent Nick Treleaven <nick geany.org> writes:
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
prev sibling parent reply kdevel <kdevel vogtner.de> writes:
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
parent reply Paul Backus <snarwin gmail.com> writes:
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
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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
Sep 01
parent evilrat <evilrat666 gmail.com> writes:
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: ```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