Get compile time string of dmd command line options "-os" & "-target"
pragma(msg, os.stringof...?); pragma(msg, target.stringof...?); what is use case for this? mixin & import Long list of version. version(Windows) mixin(import("foo_windows.enum.")); else version(AArch64) mixin(import("foo_aarch64.enum")); else static assert(0); Shorter with one line mixin(import("foo_" ~ os.stringof ~ ".enum.")); mixin(import("foo_" ~ target.stringof ~ ".enum."));
Re: SumType extraction
On Thursday, 27 June 2024 at 18:51:19 UTC, Josh Holtrop wrote: Hello all. In my application I came across a desire to store an ordered array of handles that could point to one of several different objects, and it seems like the tool I want for that is SumType. I started with something like (simplified of course): ```d class Foo {} class Bar {} My Variant package can do this type of thing https://github.com/apz28/dlang/blob/main/source/pham/var/var_variant.d#L3430 Variant[] mixedC; mixedC ~= new Foo(); mixedC ~= new Bar(); size_t foundCount; foreach (v; mixedC) { if (auto c = v.peek!Foo) { foundCount++; } } assert(foundCount == 1);
How to terminate thread under module destructor?
import core.thread.osthread : Thread; import std.stdio : writeln; __gshared static Thread th; __gshared static size_t tht; void run() { writeln("run"); while (tht == 0) {} } shared static this() { writeln("this"); th = new Thread(&run).start(); } shared static ~this() { writeln("~this"); tht = 1; } void main() { writeln("main"); }
Re: Delegates and values captured inside loops
On Sunday, 21 January 2024 at 20:13:38 UTC, An Pham wrote: On Saturday, 20 January 2024 at 15:59:59 UTC, Anonymouse wrote: I remember reading this was an issue and now I ran into it myself. ```d import std.stdio; void main() { auto names = [ "foo", "bar", "baz" ]; void delegate()[] dgs; foreach (name; names) { dgs ~= () => writeln(name); } foreach (dg; dgs) { dg(); } } ``` Expected output: `foo`, `bar`, `baz` Actual output: `baz`, `baz`, `baz` For c# reference when they fixed it: https://stackoverflow.com/questions/3168375/using-the-iterator-variable-of-foreach-loop-in-a-lambda-expression-why-fails To have a way out for old behavior by capture reference, from: dgs ~= () => writeln(name); to: dgs ~= () => writeln(&name);
Re: Delegates and values captured inside loops
On Saturday, 20 January 2024 at 15:59:59 UTC, Anonymouse wrote: I remember reading this was an issue and now I ran into it myself. ```d import std.stdio; void main() { auto names = [ "foo", "bar", "baz" ]; void delegate()[] dgs; foreach (name; names) { dgs ~= () => writeln(name); } foreach (dg; dgs) { dg(); } } ``` Expected output: `foo`, `bar`, `baz` Actual output: `baz`, `baz`, `baz` If I make `names` an `AliasSeq` it works, but I need it to be a runtime array. Is there a workaround? It is broken by design and the upper afraid to fix it because of broken backward compatible. This symptom was same as early C# and MS acknowledge it and fixed it Happy coding
Re: How to hash SHA256 from string?
On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote: ```D string appKey = "1"; ubyte[1024] data = cast(ubyte[])(appKey.dup[0..$]); sha256.put(data); Your data has garbage at the end; try sha256.put(data[0..appKey.length])
Weird floating point rounding - Bug or how to control it correctly
import std.stdio; void main() { float f = 6394763.345f; import std.format : sformat; char[80] vBuffer = void; writeln("6394763.345 = ", sformat(vBuffer[], "%.4f", f)); } Output 6394763.345 = 6394763.5000
Problem with dmd-2.104.0 -dip1000 & @safe
Getting with below error for following codes. Look like bug? onlineapp.d(61): Error: scope variable `a` assigned to non-scope parameter `a` calling `foo` @safe: struct A(S = string) { @safe: S s; void delegate() c; } struct B(S = string) { @safe: @disable this(); this(C!S c, A!S a) { this.c = c; this.a = a; } C!S foo() { return c; } A!S a; C!S c; } class C(S = string) { @safe: C!S foo(A!S a) { auto o = new Object(); return foo2(o, a); } C!S foo2(Object n, A!S a) { auto b = B!S(this, a); return b.foo(); } } unittest { static struct X { @safe: void foo3() { } } X x; A!string a; a.s = "foo"; a.c = &x.foo3; auto c = new C!string(); c.foo(a); } void main() { }
Re: what is the different bettwen typeid and .classinfo?
On Monday, 30 March 2020 at 18:20:00 UTC, Steven Schveighoffer wrote: On 3/30/20 1:06 PM, Pham wrote: Will it be the same if using "is", the reason is for function that use "nothrow" attribute? if (cc.classinfo is typeid(CC)) I don't really understand the question about "nothrow". -Steve void main() { static class Foo { int i; } static bool checkFoo() nothrow { // error when compile //return typeid(Foo) == Foo.classinfo; // ok when compile return typeid(Foo) is Foo.classinfo; } checkFoo(); }
Re: what is the different bettwen typeid and .classinfo?
On Monday, 30 March 2020 at 15:15:08 UTC, Steven Schveighoffer wrote: On 3/30/20 10:38 AM, lilijreey wrote: Hi: I write code like this ```D class Base { int base; } class CC :Base { int cc; } auto cc = new CC; //I want check cc object type is CC if (typeid(cc) == typeid(CC)) // ok == if (cc.classinfo == AstAssignNode) //error if (cc.classinfo == AstAssignNode.classinfo) // ok == ``` They are both the same. Historically they were different (ClassInfo was its own type different from TypeInfo_Class). I would recommend typeid for future-proof code (it's possible, however unlikely, that .classinfo at some point goes away). -Steve Will it be the same if using "is", the reason is for function that use "nothrow" attribute? if (cc.classinfo is typeid(CC))
Re: How to define variadic delegate with a ref/out argument?
On Friday, 17 November 2017 at 06:21:50 UTC, Jerry A. wrote: On Friday, 17 November 2017 at 05:08:23 UTC, pham wrote: struct DelegateList(Args...) { public: alias DelegateHandler = void delegate(Args args) nothrow; DelegateHandler[] items; void opCall(Args args) nothrow { foreach (i; items) i(args); } } DelegateList!(string, int) list; // Compile OK so far DelegateList!(string, int*) list2; // Compile OK so far DelegateList!(string, ref int) list3; // Compile error -> How to make it work? Thanks Pham The only way I know of is using a template which behaves like a reference. Which can be done with nullableRef I suppose. Haven't actually tried it. import std.typecons : NullableRef, nullableRef; DelegateList!(NullableRef!int)(nullableRef(some_int)); nullableRef is same as passing pointer. Using "ref" is stronger guarantee that the var is never be passed as null. variadic parameter passed by "ref" should be supported Thanks. Pham
How to define variadic delegate with a ref/out argument?
struct DelegateList(Args...) { public: alias DelegateHandler = void delegate(Args args) nothrow; DelegateHandler[] items; void opCall(Args args) nothrow { foreach (i; items) i(args); } } DelegateList!(string, int) list; // Compile OK so far DelegateList!(string, int*) list2; // Compile OK so far DelegateList!(string, ref int) list3; // Compile error -> How to make it work? Thanks Pham
Trait to identify if a type is a struct one
Is there a way to identify if a type is a struct, something like isStruct similar to isArray. struct X { } isStruct!X == true? Also, there are isAbstractClass & isFinalClass but want to check if type is a class regardless? something like isClass? Thanks Pham
Re: How to fix wrong deprecation message - dmd-2.075.1
On Wednesday, 16 August 2017 at 13:55:31 UTC, Steven Schveighoffer wrote: On 8/16/17 9:12 AM, Daniel Kozak via Digitalmars-d-learn wrote: It should not be print? AIAIK std.utf.toUTF16 is not deprecated: http://dlang.org/phobos/std_utf.html#toUTF16 OK this one is:https://github.com/dlang/phobos/blob/v2.075.1/std/utf.d#L2760 (but this one is not in doc) but this one should not be deprecated: https://github.com/dlang/phobos/blob/v2.075.1/std/utf.d#L2777 Hm.. that's a bug in the compiler. Only one is marked, but both are treated as deprecated. I'm wondering if just resolving the overload triggers the message. Please file an issue. -Steve Issue 17757 is created
File.write write extra CR character if a string has CRLF on windows
string s is multi-lines (CRLF as line break) The write function will write extra CR character for each CRLF pair -> why (bug?) import std.file; import std.stdio; string s = ...; auto fHandle = File("f:\\text.txt", "w"); // open for writing fHandle.write(s); fHandle.close();
Initialize associate array
Should codes below be compiled? import std.stdio; class Test { enum string[string] WorkAA = [ "foo": "work" ]; immutable string[string] NotWorkAA1 = [ "foo": "not work" ]; string[string] NotWorkAA2 = [ "foo": "not work" ]; } void main() { Test t = new Test(); } Test with DPaste with below error messages Result: Compilation error / Return code: 1 (Hangup) Compilation output: /d699/f49.d(11): Error: non-constant expression ["foo":"not work"] /d699/f49.d(16): Error: non-constant expression ["foo":"not work"]