Re: Is it possible to store different subclasses in one array?
On Monday, 13 April 2020 at 04:21:48 UTC, Leonardo wrote: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be? Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack() can be simplified as: if (auto weapon = cast(Weapon) gi) weapon.Attack();
Re: Is it possible to store different subclasses in one array?
On Monday, 13 April 2020 at 04:15:04 UTC, Leonardo wrote: On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet? Oh, thanks, this works. Now it seems obvious. But fitting another question, this case is only representative, if I want to use one method present in only one of these classes like this: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be? Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack()
Re: Is it possible to store different subclasses in one array?
On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet? Oh, thanks, this works. Now it seems obvious. But fitting another question, this case is only representative, if I want to use one method present in only one of these classes like this: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be?
Re: How to detect whethere if a JSON node exists
On Monday, 13 April 2020 at 02:22:33 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 02:20:39 UTC, Adnan wrote: Now in the above inner loop getStr(node["com"].str()) crashes in runtime if an array does not contain "com" node. I want to avoid that. How should I proceed? Try: if("com" in node) { } Came back here to say that I found exactly this solution. Thanks.
How to detect whethere if a JSON node exists
In the following code, I want to process an json array (returned by the value of "posts") that might or might not have "com" key. If a "com" key does not exist, I want to ignore that item in the json array. uint[string] wordTable; const auto j = parseJSON(get(link)); foreach (node; j["posts"].array()) { import std.stdio : writeln; import std.utf : decode; import std.algorithm: splitter; foreach(word; getStr(node["com"].str()).splitter(' ')) { // here! if (word != "") wordTable.require(word, 0)++; } } Now in the above inner loop getStr(node["com"].str()) crashes in runtime if an array does not contain "com" node. I want to avoid that. How should I proceed?
Re: How to detect whethere if a JSON node exists
On Monday, 13 April 2020 at 02:20:39 UTC, Adnan wrote: Now in the above inner loop getStr(node["com"].str()) crashes in runtime if an array does not contain "com" node. I want to avoid that. How should I proceed? Try: if("com" in node) { }
Re: Is it possible to store different subclasses in one array?
On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet?
Is it possible to store different subclasses in one array?
Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? public class BaseItem{ public string name = ""; } public class Weapon : BaseItem{ public int damage = 10; } public class Potion : BaseItem{ public int hpRestore = 50; } var GameItems = new List(); GameItems.Add(new Weapon()); GameItems.Add(new Potion());
Re: linker aliases to carry dlang attributes for externs
On Sunday, 12 April 2020 at 23:14:42 UTC, Bruce Carneal wrote: Could dlang compilers emit aliases for extern(C) and extern(C++) routines that would carry dlang specific information? (@safe, @nogc, nothrow, ...) I'm thinking two symbols. The first as per normal C/C++, and the second as per normal dlang with a "use API {C, C++, ...}" suffix. ABI, not API.
linker aliases to carry dlang attributes for externs
Could dlang compilers emit aliases for extern(C) and extern(C++) routines that would carry dlang specific information? (@safe, @nogc, nothrow, ...) I'm thinking two symbols. The first as per normal C/C++, and the second as per normal dlang with a "use API {C, C++, ...}" suffix.
Re: odd atomicOp errors from vibe-core
On 4/12/20 6:30 AM, Iain Buclaw wrote: This is a regression caused by https://github.com/dlang/dmd/pull/10711 Thanks for finding that! I wasn't sure where the culprit was. -Steve
Re: Using the functions "map" and "any" on tuples in compile time.
On Sunday, 12 April 2020 at 12:42:40 UTC, Harry Gillanders wrote: On Sunday, 12 April 2020 at 11:17:39 UTC, realhet wrote: I only remembered the __traits(identifier...), but completely forgot about the getMember. And I never heard of anySatisfy. My JSON serializer is beautiful now. Thank You very much!
Re: How user dub packages in dmd without dub.exe ?
On Sunday, 5 April 2020 at 14:19:40 UTC, WebFreak001 wrote: On Sunday, 5 April 2020 at 14:02:19 UTC, Baby Beaker wrote: On Saturday, 4 April 2020 at 21:54:34 UTC, Andre Pany wrote: On Saturday, 4 April 2020 at 20:21:03 UTC, Marcone wrote: [...] [...] If you can copy the D packages from the Dub packages into your main source folder, the command maybe is just: [...] When I run with command dmd -i -run app.d I get this error: app.d(4): Error: module `vibe` is in file 'vibe\vibe.d' which cannot be read did you do the "copy the D packages from the Dub packages into your main source folder" part? No. Why?
Re: Using the functions "map" and "any" on tuples in compile time.
On Sunday, 12 April 2020 at 11:17:39 UTC, realhet wrote: Hello, anyone can help me make this better? The functionality I want to achieve is: While serializing the fields of a struct, I want it to check the @STORED UDA on every field. If there is no fields are marked with @STORED, that means every field must be serialized. Otherwise only the marked one. I had problems using map and any, so I come up with this lame foreach version: It works, but I think it does it in runtime. bool anySTORED = false; static foreach(fieldName; FieldNameTuple!T) mixin("anySTORED |= hasUDA!(data.*, STORED);".replace("*", fieldName)); static foreach(fieldName; FieldNameTuple!T){{ mixin("const thisSTORED = hasUDA!(data.*, STORED);".replace("*", fieldName)); if(thisSTORED || !anySTORED) mixin("streamAppend_json!(dense, fieldName)(st, data.*, nextIndent);".replace("*", fieldName)); }} Thanks in advance! (ps: I just love string mixins, I know :D) Using a compile-time tuple as a range is easy, turn it into an array via an array literal (surround it in square bracket), e.g. struct Foo { int a; int b; } pragma(msg, [FieldNameTuple!Foo].map!(f => f ~ "_").array()); However, if you were to try that with `any` for `hasUDA`, wherein the arguments for `any`'s predicate are used for `hasUDA`'s template parameters, you'll find that it won't compile. That's because `any`'s predicate is a runtime function, executed at compile-time via CTFE, so the argument technically isn't known at compile-time for the `hasUDA` template, e.g. struct Foo { int a; int b; } enum STORED; enum bool anyStored = [FieldNameTuple!Foo].any!( f => hasUDA!(__traits(getMember, Foo, f), STORED) ); The solution to that is to define a template predicate, and use std.meta.anySatisfy, instead of `any`. Which would accomplish what you want to do, with something like so: string serialiseFields (T) (auto ref T instance) { enum bool hasStored (string fieldName) = hasUDA!(__traits(getMember, T, fieldName), STORED); enum fields = FieldNameTuple!T; static if (anySatisfy!(hasStored, fields)) { enum fieldsToSerialise = Filter!(hasStored, fields); } else { enum fieldsToSerialise = fields; } string serialise (string name, T) (auto ref T value) { return format!(name ~ " = %s")(value); } string serialised; static foreach (field; fieldsToSerialise) { serialised ~= serialise!field(__traits(getMember, instance, field)) ~ "\n"; } return serialised; } --- This source code in this reply is licensed under the terms of Creative Commons CC0 1.0.
Using the functions "map" and "any" on tuples in compile time.
Hello, anyone can help me make this better? The functionality I want to achieve is: While serializing the fields of a struct, I want it to check the @STORED UDA on every field. If there is no fields are marked with @STORED, that means every field must be serialized. Otherwise only the marked one. I had problems using map and any, so I come up with this lame foreach version: It works, but I think it does it in runtime. bool anySTORED = false; static foreach(fieldName; FieldNameTuple!T) mixin("anySTORED |= hasUDA!(data.*, STORED);".replace("*", fieldName)); static foreach(fieldName; FieldNameTuple!T){{ mixin("const thisSTORED = hasUDA!(data.*, STORED);".replace("*", fieldName)); if(thisSTORED || !anySTORED) mixin("streamAppend_json!(dense, fieldName)(st, data.*, nextIndent);".replace("*", fieldName)); }} Thanks in advance! (ps: I just love string mixins, I know :D)
Re: odd atomicOp errors from vibe-core
On Friday, 10 April 2020 at 01:54:14 UTC, Steven Schveighoffer wrote: I'm building a library that uses vibe-core as an indirect dependency. Specifically, I'm testing the library with dub test. A very odd thing happens as I'm picking off compiler errors one at a time. After all the errors that I created are listed, I get this list: [--snip--] Error: template core.atomic.atomicOp cannot deduce function from argument types !("-=")(shared(uint), int), candidates are: /home/steves/.dvm/compilers/dmd-2.091.0/linux/bin/../../src/druntime/import/core/atomic.d(543,14): atomicOp(string op, T, V1)(ref shared T val, V1 mod) with op = "-=", T = uint, V1 = int must satisfy the following constraint: __traits(compiles, mixin("*cast(T*)&val" ~ op ~ "mod")) [--snip--] ../../../.dub/packages/vibe-core-1.9.0/vibe-core/source/vibe/core/sync.d(2006,22): Error: template core.atomic.atomicOp cannot deduce function from argument types !("+=")(shared(uint), int), candidates are: And then it just abruptly ends there. I'm not sure why all these errors come out. Looking at the code, it seems to have nothing to do with my code. Once I fix my code errors (which are legitimate errors that I made), then these mysteriously go away. I'm not calling any of these functions that it's spitting out. Why are these being triggered? Why do they all of a sudden go away? Does anyone else see this? Is it specifically something with vibe-core? Is it something to do with dub? This is a regression caused by https://github.com/dlang/dmd/pull/10711