How to terminate thread under module destructor?

2024-03-09 Thread An Pham via Digitalmars-d-learn
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().start();

Re: Delegates and values captured inside loops

2024-01-21 Thread An Pham via Digitalmars-d-learn
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()[]

Re: Delegates and values captured inside loops

2024-01-21 Thread An Pham via Digitalmars-d-learn
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 ~= () =>

Re: How to hash SHA256 from string?

2023-12-02 Thread An Pham via Digitalmars-d-learn
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

Weird floating point rounding - Bug or how to control it correctly

2023-09-13 Thread An Pham via Digitalmars-d-learn
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

2023-06-08 Thread An Pham via Digitalmars-d-learn
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) {

Re: what is the different bettwen typeid and .classinfo?

2020-03-30 Thread Pham via Digitalmars-d-learn
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".

Re: what is the different bettwen typeid and .classinfo?

2020-03-30 Thread Pham via Digitalmars-d-learn
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 ==

Re: How to define variadic delegate with a ref/out argument?

2017-11-19 Thread pham via Digitalmars-d-learn
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 {

How to define variadic delegate with a ref/out argument?

2017-11-16 Thread pham via Digitalmars-d-learn
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

Trait to identify if a type is a struct one

2017-10-18 Thread pham via Digitalmars-d-learn
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

2017-08-16 Thread Pham via Digitalmars-d-learn
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

File.write write extra CR character if a string has CRLF on windows

2016-10-06 Thread Pham via Digitalmars-d-learn
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

2016-02-29 Thread pham via Digitalmars-d-learn
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 =