Re: How should I return multiple const values from a function?
On Tuesday, 3 January 2023 at 01:56:10 UTC, Paul Backus wrote: On Monday, 2 January 2023 at 23:25:48 UTC, Charles Hixson wrote: They syntax I wanted was something like: bool func (const out Key k, const out Val v) { k = this.key.dup; v = this.val.dup; return true; } This works for me: ```d template Fun(Key, Value) { import std.typecons; alias Tup = Tuple!(const(Key), const(Value)); enum Fun = (Key k, Value v) => Tup(k, v);/* auto Fun(Key k, Value v) { return Tup(k, v); }//*/ } string k; uint v; auto res = Fun(k, v); static assert(is(typeof(res[0]) == const(string))); // false static assert(is(typeof(res[1]) == const(uint))); //false ``` I rewrote the code to return the enum. However, static asserts return false. What could this cause? P.S. Actually the code works when we don't use enum. SDB@79
Re: How should I return multiple const values from a function?
On 1/2/23 17:56, Paul Backus via Digitalmars-d-learn wrote: return Tuple!(const(Key), const(Value))(k, v); Great! OK, now the code is: auto findFirst () { if (root is null) { Key k = Key.init; Val v = Val.init; return Tuple!(const(Key), const(Val))(k, v); } Node n = root; while (n.lft !is null) n = n.lft; auto k = cast(const)(n.key); auto v = cast(const)(n.val); return Tuple!(const(Key), const(Val))(k, v); } //nd bool findFirst (out Key k, out Val v) I couldn't show that previously because the various things I was trying were too different. -- Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.
Re: How should I return multiple const values from a function?
On Monday, 2 January 2023 at 23:25:48 UTC, Charles Hixson wrote: They syntax I wanted was something like: bool func (const out Key k, const out Val v) { k = this.key.dup; v = this.val.dup; return true; } This works for me: import std.typecons; auto func(Key, Value)(Key k, Value v) { return Tuple!(const(Key), const(Value))(k, v); } void main() { string k; uint v; auto result = func(k, v); static assert(is(typeof(result[0]) == const(string))); static assert(is(typeof(result[1]) == const(uint))); }
Re: How should I return multiple const values from a function?
On 1/2/23 15:14, Paul Backus via Digitalmars-d-learn wrote: On Monday, 2 January 2023 at 22:53:13 UTC, Charles Hixson wrote: I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them. It's hard to say where exactly you're going wrong if you only post the error message, without the code that produced it. If you post your code (or a simplified version with the same problem), I'm sure someone will be able to help you. O. Key and Val are template parameters, and Key has a less method defined. -- Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.
Re: How should I return multiple const values from a function?
On 1/2/23 15:14, Paul Backus via Digitalmars-d-learn wrote: On Monday, 2 January 2023 at 22:53:13 UTC, Charles Hixson wrote: I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them. It's hard to say where exactly you're going wrong if you only post the error message, without the code that produced it. If you post your code (or a simplified version with the same problem), I'm sure someone will be able to help you. I don't really know where to start. dup doesn't reliably work on template parameters. Assignment returns things that may have internal pointers. The code is 5-6 lines of testing this approach or that, and every single one of them has failed. Some approaches will work with strings, but not with ints, some with ints but not with strings, some may return pointers to internal data. What I'm trying to do is return data that will be const or immutable from a function. They syntax I wanted was something like: bool func (const out Key k, const out Val v) { k = this.key.dup; v = this.val.dup; return true; } but that fails six or seven different ways. -- Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.
Re: How should I return multiple const values from a function?
On Monday, 2 January 2023 at 22:53:13 UTC, Charles Hixson wrote: I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them. It's hard to say where exactly you're going wrong if you only post the error message, without the code that produced it. If you post your code (or a simplified version with the same problem), I'm sure someone will be able to help you.
How should I return multiple const values from a function?
I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them. I don't want the returned values to be able to be used to modify the values held in the table. const out is an illegal parameter type. dup doesn't work on ints. So I tried to return a tuple, but cast(const) doesn't work on strings. (Error: cannot implicitly convert expression `tuple(k, v)` of type `Tuple!(string, string)` to `Tuple!(const(string), const(string))` Sometimes it remembers that I've done the cast, and sometimes it doesn't. The code was: return tuple (cast(const)(Key.init), cast(const)(Val.init)); I've no idea why it was talking about type `Tuple!(string, string)`, since in the test the types were (string, uint). I feel like I'm overlooking something obvious, but I've no idea what. I really just wanted to return dups, but those don't work if one of the types might be an int. (Though I should have been trying to return idups. O, well.) -- Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.
Re: Address of a class object
Thank you, Teoh, Ali, & Matheus
Re: Is there such a JSON parser?
On Monday, 2 January 2023 at 21:36:10 UTC, Steven Schveighoffer wrote: On 1/1/23 6:28 PM, torhu wrote: I need to parse some JSON data into various data structures, so I'm looking for a parser based on events or ranges. One that doesn't just load the file and build a data structure that represents the whole thing. So not std.json, at least. It's pretty rough-edged, but https://code.dlang.org/packages/jsoniopipe will do this. It has mechanisms to jump to specific object members, and to rewind to a cached point. It does not use any intermediate representation. -Steve Thank, I can check it out.
Re: Is there such a JSON parser?
On 1/1/23 6:28 PM, torhu wrote: I need to parse some JSON data into various data structures, so I'm looking for a parser based on events or ranges. One that doesn't just load the file and build a data structure that represents the whole thing. So not std.json, at least. It's pretty rough-edged, but https://code.dlang.org/packages/jsoniopipe will do this. It has mechanisms to jump to specific object members, and to rewind to a cached point. It does not use any intermediate representation. -Steve
Re: Is there such a JSON parser?
On Monday, 2 January 2023 at 14:56:27 UTC, SealabJaster wrote: Are you asking for a SAX-styled parser for JSON? I have an upcoming project (about 3-6 months away) that could make use of this as well. If you need someone to try it out please let me know and I'll give it a spin. Good luck on your library. Cheers,
Re: Handling CheckBox state changes in DLangUI
On Saturday, 31 December 2022 at 02:40:49 UTC, Daren Scot Wilson wrote: The compiler errors I get are, for no '&' and with '&': Error: function `app.checkbox_b_clicked(Widget source, bool checked)` is not callable using argument types `()` Error: none of the overloads of `opAssign` are callable using argument types `(bool function(Widget source, bool checked))` If checkbox_b_clicked is a non-static nested function or non-static method, taking the address of it should result in a delegate, not a function pointer. You can check what it is like this: writeln(typeid(&checkbox_b_clicked));
Re: Is there such a JSON parser?
On Monday, 2 January 2023 at 14:56:27 UTC, SealabJaster wrote: Are you asking for a SAX-styled parser for JSON? Yes, I actually want to replace a SAX parser.
Re: Is there such a JSON parser?
On Monday, 2 January 2023 at 05:44:33 UTC, thebluepandabear wrote: You might want to try the following: https://github.com/libmir/asdf I had a look at that, but I think it just loads the whole file into it's own data structure. And then you can use attributes to get it to fill structs with data, but that's too basic for my needs.
Re: Is there such a JSON parser?
On Sunday, 1 January 2023 at 23:28:12 UTC, torhu wrote: I need to parse some JSON data into various data structures, so I'm looking for a parser based on events or ranges. One that doesn't just load the file and build a data structure that represents the whole thing. So not std.json, at least. Are you asking for a SAX-styled parser for JSON? Similarly - what would you like to see from a full-featured JSON module? For context: I'm currently (very slowly) working on a -betterC library. The JSON lexer is able to work off of buffered data streams instead of flat in-memory arrays. So even if what you're looking for doesn't exist right now, I'd like to try and (again; very slowly) change that.