Re: Scope & Structs

2024-10-13 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 13 October 2024 at 05:12:32 UTC, Salih Dincer wrote: Can we say that structs are in the stack (LIFO) as long as we do not use the new operator? Just to note that `new` does not give you a struct, it gives a struct pointer. Structs use the stack when declared inside a stack-allocate

Re: Scope & Structs

2024-10-12 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 12 October 2024 at 12:10:17 UTC, Salih Dincer wrote: On Saturday, 12 October 2024 at 12:02:04 UTC, Salih Dincer wrote: ... even if I call the structure with the new operator. But if I stop using classes, scope doesn't work properly! Declaring a `scope SomeClass` initialized with `

Re: Why is this not allowed?

2024-10-09 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 9 October 2024 at 01:16:29 UTC, Boaz Ampleman wrote: I'm not a big fan of using declarations as type either: ```d bool doWeWantThat( struct{ int notsure;} p) { return false; } ``` Here `(int notsure) p` would read much better. And we want tuple syntax in the language anyway

Re: Why is this not allowed?

2024-10-07 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 7 October 2024 at 08:05:59 UTC, ryuukk_ wrote: I'm working on it TODAY, therefore i need a today's solution ```d import std.typecons; struct EntityDef { Tuple!(int, "hp") stats; } void main() { EntityDef ed; int x = ed.stats.hp; } ```

Re: Genuine copy of an element from an associative array

2024-10-06 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 3 October 2024 at 12:23:27 UTC, Holzofen wrote: ```d uint64 sod_decompose(const uint64 n, const uint64 mod, const ref uint64[] primes, const ref uint64[uint64][uint64] factorials) { auto result = factorials[n]; for (uint64 k = 2; k < n - 1; k++) {

Re: Why is this not allowed?

2024-10-06 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 5 October 2024 at 17:26:59 UTC, Steven Schveighoffer wrote: It’s the semicolon. As soon as the closing brace, the declaration is over. You would have to invent new syntax. Maybe type tuple syntax will support this: ```d struct EntityDef { (int hp) stats; } EntityDef ed; int x

Re: need help to work around float union non-zero init

2024-09-20 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 20 September 2024 at 09:38:54 UTC, Dakota wrote: I need my struct defined as `isZeroInit`, so can I can import them as `di` file. (this also reduce build size) But I find this problem with float inside union: ```d struct test_t { union { int i32; float f32;

Re: assert

2024-09-11 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 11 September 2024 at 08:08:45 UTC, ryuukk_ wrote: It is a bug, don't claim it is not, the compiler gives the wrong information, wich lead to a confused user You don't want confused users, you want compiler say what's up, if i type assert, it should assert, end of the story The

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-09-02 Thread Nick Treleaven via Digitalmars-d-learn
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.

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-09-02 Thread Nick Treleaven via Digitalmars-d-learn
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

Re: Associative Array, get value instead of poiter using `if (auto ..)`, possible?

2024-09-02 Thread Nick Treleaven via Digitalmars-d-learn
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.deb

Re: Understanding the Behavior of i + ++i in D Language

2024-08-23 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 23 August 2024 at 09:42:38 UTC, Nick Treleaven wrote: C++: undefined, could be `6 + 6` if the increment is done first. g++ gives me a warning with `-Wall`: You asked about C, for some reason I used C++. But it's the same in C, and the error happens with `gcc -Wall`.

Re: Understanding the Behavior of i + ++i in D Language

2024-08-23 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 23 August 2024 at 08:58:16 UTC, Me'vâ wrote: ``` import std.stdio:writeln; void main() { int i = 5; writeln("Result: ", i + ++i); } ``` When I run this, it surprisingly outputs 11. I tried something similar in C before and it gave me 12. I’m curious, why is there a differen

Re: create fixed length string of characters

2024-08-16 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 16 August 2024 at 16:30:09 UTC, Jonathan M Davis wrote: Well, you if you use dup, you're asking for a mutable array, whereas if you use idup, you're asking for an immutable array. Yes, `idup` may be needed e.g. for overloads varying on mutability. Whether the result of dup is then

Re: create fixed length string of characters

2024-08-16 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote: On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln

Re: create fixed length string of characters

2024-08-16 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: Is there an easy way to create a 60 character string in D? Like in Python... ul = '-'*60 2 ways: ```d // use a fixed array: immutable char[60] a = '-'; string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] othe

Re: Any way to automatically convert structs on return?

2024-08-01 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 1 August 2024 at 08:46:00 UTC, IchorDev wrote: P.S. You might want to put `value = void`, otherwise it’ll always be default-constructed. Doing `= void` can violate the assumptions of a destructor of T. Nullable uses a union to store T, so it can decide when to call the destructor

Re: Prevent self-comparison without taking the address

2024-07-25 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 25 July 2024 at 15:06:35 UTC, IchorDev wrote: I think your function most likely has a safe interface, so it can be marked as `@trusted` as-per [the spec](https://dlang.org/spec/function.html#safe-interfaces). Just to mention that with -dip1000, taking the address of variables is

Re: Array concatenation & optimisation

2024-07-22 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 21 July 2024 at 10:33:38 UTC, Nick Treleaven wrote: On Sunday, 21 July 2024 at 05:43:32 UTC, IchorDev wrote: Does this mean that array literals are *always* separately allocated first, or is this usually optimised out? My understanding is that they do not allocate if used to

Re: Array concatenation & optimisation

2024-07-21 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 21 July 2024 at 10:33:38 UTC, Nick Treleaven wrote: For instance, will this example *always* allocate a new dynamic array for the array literal, and then append it to the existing one, even in optimised builds? ```d void append(ref int[] a){ a ~= [5, 4, 9]; } ``` If there

Re: Tuple deconstruction in Phobos

2024-07-21 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 21 July 2024 at 04:05:52 UTC, IchorDev wrote: On Saturday, 20 July 2024 at 20:48:29 UTC, Nick Treleaven wrote: Instead of the `tie` assignment, you can just do: ```d import std.meta; AliasSeq!(y, x) = tupRetFn().expand; ``` And here I was trying to use comma

Re: Array concatenation & optimisation

2024-07-21 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 21 July 2024 at 05:43:32 UTC, IchorDev wrote: Obviously when writing optimised code it is desirable to reduce heap allocation frequency. With that in mind, I'm used to being told by the compiler that I can't do this in `@nogc` code: ```d void assign(ref int[4] a) @nogc{ a[] = [1,3,6

Re: Tuple deconstruction in Phobos

2024-07-20 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 20 July 2024 at 14:02:21 UTC, IchorDev wrote: Why does Phobos not provide a method to easily deconstruct tuples? Here's a trivial implementation: ... tie!(y, x) = tupRetFn().expand; writeln(x,", ",y); } ``` Not having this is like if Phobos didn't have `AliasSeq`. Y

Re: need help to check symbol is static variable or not

2024-07-15 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 15 July 2024 at 06:44:12 UTC, Dakota wrote: ```d struct type_s { union { struct { int a; int b; } c; }; }; ``` `type c` will return false with `enum isStatic(alias V) = __traits(compiles, { enum v = V; });` I put `type_s` in

Re: need help to check symbol is static variable or not

2024-07-01 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 1 July 2024 at 07:32:30 UTC, Dakota wrote: this code give error: ```d static if( !__traits(compiles, mixin("enum v = V;")) ) { enum v = V; } ``` __traits(compiles, ...) can't check if a declaration is valid directly. You have to wrap it in a function literal expression: ```d

Re: Default struct constructors if a struct member is a union

2024-06-30 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 29 June 2024 at 23:33:41 UTC, solidstate1991 wrote: S foo0 = S(TypeEnum.Integer32, S(20)); //Ugly, but works S foo1 = S(TypeEnum.Integer64, S(20L)); //Error: cannot Did you mean `U(20)`? The 20 applies to the first field of the union, i32. `U(20L)` also works and (I think) it doe

Re: Why `foo.x.saa.aa` and `foo.y.saa.aa` is the same? `shared_AA.saa` should still be instance variable, not class variable, right?

2024-06-25 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 25 June 2024 at 02:16:25 UTC, mw wrote: Why `foo.x.saa.aa` and `foo.y.saa.aa` is the same? (and of course print out the same contents). `shared_AA.saa` should still be instance variable, not class variable, right? `saa` is an instance variable, but both `foo.x.saa` and `foo.y.sa

Re: How to generate a random number from system clock as seed

2024-06-08 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 8 June 2024 at 16:09:04 UTC, monkyyy wrote: rng is an optional parameter, `uniform(0,100).writeln;` alone works; the docs not telling you that is really bad They do tell you: urng (optional) random number generator to use; if not specified, defaults to rndGen That overload is

Re: Unintentional sharing?

2024-06-06 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 6 June 2024 at 17:49:39 UTC, Andy Valencia wrote: I was using instance initialization which allocated a new object. My intention was this initialization would happen per-instance, but all instances appear to share the same sub-object? That is, f1.b and f2.b appear to point to a s

Re: How to pass in reference a fixed array in parameter

2024-06-05 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 5 June 2024 at 09:24:23 UTC, evilrat wrote: for simple cases like this it might work, but 2d array is not even contiguous, A 2D static array is contiguous: https://dlang.org/spec/arrays.html#rectangular-arrays D static arrays, while using the same syntax, are implemented as a fi

Re: How to pass in reference a fixed array in parameter

2024-06-05 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 5 June 2024 at 10:27:47 UTC, Nick Treleaven wrote: foreach (i, row; maze) slices[i] = row; Sorry that assignment was wrong (edited at last minute). Fixed: ```d import std.stdio; alias s_cell = int; void main() { writeln("Maze generation demo"); s_cell [

Re: How to pass in reference a fixed array in parameter

2024-06-05 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 5 June 2024 at 10:27:47 UTC, Nick Treleaven wrote: //~ void print_maze ( s_cell [][] maze... ) I meant to delete that line!

Re: How to pass in reference a fixed array in parameter

2024-06-05 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 4 June 2024 at 12:22:23 UTC, Eric P626 wrote: ~~~ void main() { writeln("Maze generation demo"); s_cell [5][5] maze; print_maze (maze); } void print_maze ( s_cell [][] maze ) { } ~~~ This is how to do it without GC allocations (I have used `int` instead for demo purposes)

Re: How to pass in reference a fixed array in parameter

2024-06-05 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 5 June 2024 at 06:22:34 UTC, Eric P626 wrote: Now according to the book, it's possible to assign a slice from a fixed array. This code will compile: ~~~ int[12] monthDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; int[] a_slice = monthDays; ~~~ The element types are

Re: bool passed by ref, safe or not ?

2024-06-05 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 5 June 2024 at 09:09:40 UTC, Kagamin wrote: On Wednesday, 5 June 2024 at 01:18:06 UTC, Paul Backus wrote: The only safe values for a `bool` are 0 (false) and 1 (true). AFAIK that was fixed and now full 8-bit range is safe. `cast(bool) someByte` is fine - that doesn't reinterpre

Re: What prevents ImportC from using .h directly?

2024-05-13 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 12 May 2024 at 21:34:30 UTC, Chris Piker wrote: So, why does ImportC need *.c files exclusively? I'm sure it solves a problem, but I don't know what that problem is. Support for headers has been worked on, but had to be reverted: https://issues.dlang.org/show_bug.cgi?id=23479

Re: "in" operator gives a pointer result from a test against an Associative Array?

2024-05-10 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 10 May 2024 at 15:23:39 UTC, Andy Valencia wrote: On Friday, 10 May 2024 at 03:07:43 UTC, Steven Schveighoffer wrote: Yes, we say that a type has "truthiness" if it can be used in a condition (`while`, `if`, `assert`, etc). So if I may ask for one more small clarification... WRT "t

Re: Why is Phobos `Flag` so overthought ?

2024-05-09 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 8 May 2024 at 10:24:07 UTC, Nick Treleaven wrote: Named arguments are optional, so I don't see how they could make Flag redundant. Actually, an external tool could detect when a bool is passed as an argument to a function and warn when not done with a named argument. This

Re: Why is Phobos `Flag` so overthought ?

2024-05-09 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 9 May 2024 at 13:40:56 UTC, cc wrote: It's pointless mandatory verbosity. StopWatch ctor only takes one boolean argument. It doesn't *need* to specify what it relates to. You either already know, or you have to look it up anyway. Flags made sense when you might get the order of

Re: Why is Phobos `Flag` so overthought ?

2024-05-08 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 8 May 2024 at 04:27:13 UTC, cc wrote: It doesn't allow a simple boolean to be used as an argument, or any other Flag as they are different instantiations of a template rather than equivalent aliases. It is however awful, cumbersome, annoying design and needs to be completely phase

Re: How can I put the current value of a variable into a delegate?

2024-05-06 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 6 May 2024 at 06:29:49 UTC, Liam McGillivray wrote: Here's a line that caused a bug that took me awhile to find: ``` foreach(card; unitCards) card.submitted = delegate() => selectUnit(card.unit); ``` I think you can do: ```d import std.algorithm.iteration : each; unitCards.

Re: How can I put the current value of a variable into a delegate?

2024-05-06 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 6 May 2024 at 06:29:49 UTC, Liam McGillivray wrote: This is because the delegate assignment causes the local `card` variable to remain alive. The delegate that's assigned is linked to this variable itself, not the value at the time that the delegate is assigned. This is https://iss

Re: Turning fixed sized array into tuple

2024-05-04 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 4 May 2024 at 16:58:00 UTC, Dmitry Olshansky wrote: So I have a function: ```d size_t awaitAny(T...)(T args) { ... } ``` And I have: ``d Event*[4] events; `` How do I pass all 4 of events to awaitAny as tuple of arguments? Use `awaitAny(events.tupleof)`? https://dlang.org/spec/a

Re: Phobos function to remove all occurances from dynamic array?

2024-05-01 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 1 May 2024 at 01:09:33 UTC, Liam McGillivray wrote: I get compiler errors when using it on other array types. I've tried using it to replace occurrences of a certain object in an array with [] in order to remove all occurrences, but it's not allowed. Can you post a code example?

Re: Challenge Tuples

2024-04-27 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 27 April 2024 at 15:32:40 UTC, Nick Treleaven wrote: On Saturday, 27 April 2024 at 11:55:58 UTC, Basile B. wrote: foreach const e in u do if echo(is, e, T) do result += e; static if (is(typeof(e) == int)) r += e; Actually

Re: Challenge Tuples

2024-04-27 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 27 April 2024 at 11:55:58 UTC, Basile B. wrote: Here's [STYX](https://gitlab.com/styx-lang/styx) solution: function sum[T,U](U u): u32 I think you meant `: T`. { var T result; foreach const e in u do if echo(is, e, T) do resul

Re: std.traits.ParameterIdentifierTuple problem

2024-04-01 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 31 March 2024 at 23:05:44 UTC, Carl Sturtivant wrote: Yes, it's not possible to instantiate a function type. But with extern it seems the semantics is fine as a function is not being instantiated. It is merely associating a name with a type: in what sense is this instantiation in a

Re: std.traits.ParameterIdentifierTuple problem

2024-03-31 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 30 March 2024 at 22:37:53 UTC, Carl Sturtivant wrote: I'm inclined to a view that keeps more "it just works" options open. Regard the parameter names as a part of the type (which I am very grateful for them being currently) and just regard part of the definition of "type equality"

Re: std.traits.ParameterIdentifierTuple problem

2024-03-30 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 30 March 2024 at 21:45:34 UTC, Nick Treleaven wrote: On Saturday, 30 March 2024 at 21:25:45 UTC, Carl Sturtivant wrote: OK, so how can I get them? Am I forced to take that string and parse it with CTFE? Lookup the source of ParameterIdentifierTuple and change `FunctionTypeOf

Re: std.traits.ParameterIdentifierTuple problem

2024-03-30 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 30 March 2024 at 21:25:45 UTC, Carl Sturtivant wrote: OK, so how can I get them? Am I forced to take that string and parse it with CTFE? Lookup the source of ParameterIdentifierTuple and change `FunctionTypeOf!func` to just `func` inside the first `static if`.

Re: std.traits.ParameterIdentifierTuple problem

2024-03-30 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 30 March 2024 at 19:23:07 UTC, Carl Sturtivant wrote: $ dmd -c bug1.d int(int num, string name, int) ["", "", ""] bug1.d(9): Error: static assert: "wrong!" ``` Please explain. How do I get the names of the identifiers out of a parameter list at compile time reliably? Although `.s

Re: Mutate immutable inside shared static constructor

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis wrote: Yes, it's a bug. It's a clear violation of the type system if a non-mutable variable is ever given a value more than once. It should be initialized, and then it should be treated as illegal to ever assign to it - or to do anyth

Mutate immutable inside shared static constructor

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
I've not used static constructors before, but it seems like the following should not be allowed: ```d import std.stdio; immutable int x; @safe shared static this() { x.writeln(); // 0 x = 5; x.writeln(); // 5 x = 6; x++; assert(x == 7); } ``` Should I file a bug to requ

Re: Mutability issue

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 23 March 2024 at 19:30:29 UTC, Menjanahary R. R. wrote: for (T candidate = T(5); candidate * candidate <= n; candidate += T(6)) { When T is `const int`, the above code declares and initializes a constant variable: ```d const int candidate = const int(5); ``` Then, at the end

Re: length's type.

2024-02-12 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 9 February 2024 at 15:19:32 UTC, bachmeier wrote: It's been discussed many, many times. The behavior is not going to change - there won't even be a compiler warning. (You'll have to check with the leadership for their reasons.) Was (part of) the reason because it would disrupt exist

Re: Providing implicit conversion of

2024-01-23 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 22 January 2024 at 19:49:19 UTC, Siarhei Siamashka wrote: The two's complement wraparound behavior mandated by the D language spec is a non-technical political decision, intended to make life easier for the DMD compiler developers, but ignoring the needs of the users. Actually it i

Re: Providing implicit conversion of - memory-safety

2024-01-23 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 22 January 2024 at 19:11:50 UTC, Siarhei Siamashka wrote: On Monday, 22 January 2024 at 16:39:10 UTC, Nick Treleaven wrote: Memory safety issues are a worse class of bug than arithmetic bugs. The latter are reproducible if you feed them the same input. Memory safety bugs are

Re: Providing implicit conversion of

2024-01-22 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 22 January 2024 at 01:14:06 UTC, Steven Schveighoffer wrote: The language should not allow unary unsigned anything. This is unlikely to get fixed, just due to the nature of D's philosophy when it comes to C compatibility. It would also break a lot of existing code. I think the b

Re: Permutations of array (slice) ?

2023-12-13 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 12 December 2023 at 16:21:46 UTC, Kevin Bailey wrote: On Tuesday, 12 December 2023 at 15:20:23 UTC, Paul Backus wrote: But unfortunately, the code shown now prints 120 lines of: b 120 being suspiciously equal to 5!. The documentation[2] seems to imply that this should be: baa

Re: union default initialization values

2023-12-06 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 6 December 2023 at 12:38:35 UTC, Nick Treleaven wrote: Correct. So I expected a NaN output for x. However, I wasn't expecting lo == 13835058055282163712 and hi == 32767 where x is of type real, or lo == 9221120237041090560 and hi = 0 where x is of type double. Based o

Re: union default initialization values

2023-12-06 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 5 December 2023 at 19:47:38 UTC, confuzzled wrote: On 12/6/23 4:28 AM, Adam D Ruppe wrote: On Tuesday, 5 December 2023 at 19:24:51 UTC, confuzzled wrote: Given the following union union F {     double x;     struct {     ulong lo;     ulong hi;     } } The default value o

Re: Inversion of conditional compilation statements

2023-12-02 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 2 December 2023 at 15:03:25 UTC, ryuukk_ wrote: I wish we could use ``version`` as expression, to void the repetition: ```D import std.stdio; enum HasTest = version (Test) ? true : false; Tomek Sowiński wrote this template: ```d enum bool isVersion(string ver) = !is(typeof({

Re: D: Convert/parse uint integer to string. (@nogc)

2023-11-27 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 27 November 2023 at 12:34:30 UTC, Nick Treleaven wrote: On Friday, 24 November 2023 at 09:35:00 UTC, BoQsc wrote: You can use std.conv.toChars: ```d void main() @nogc { int n = 515; import std.conv; char[10] s = 0; auto r = n.toChars(); assert(r.length < s.len

Re: D: Convert/parse uint integer to string. (@nogc)

2023-11-27 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 24 November 2023 at 09:35:00 UTC, BoQsc wrote: I tried to look into https://dlang.org/phobos/std_conv.html Most of the functions inside `std.conv` seem to be dependant on [Garbage Collection](https://dlang.org/spec/garbage.html). And I couldn't find a straightforward way to produce

Re: Struct copy constructor with inout

2023-11-16 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 13:58:17 UTC, Paul Backus wrote: On Tuesday, 14 November 2023 at 13:41:32 UTC, Steven Schveighoffer wrote: ``` Error: copy constructor `testinoutctor.S1.this(ref const(S1) s) const` is not callable using argument types `(const(S1))` ``` I'm not sure what this m

Re: dlang.org/spec/function.html#pure-functions example

2023-10-28 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 16 October 2023 at 18:05:04 UTC, Paul wrote: On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis wrote: look like? Types can have static members. Basically what it comes down to is that outside of immutable data, pure functions only have access to their arguments and to

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-13 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 10 October 2023 at 11:45:25 UTC, Dennis wrote: ```D enum itoa(int i) = i.stringof; static foreach(i; 0 .. 10) { mixin(create_fn!(itoa!i)); } ``` You can also do it using a string mixin: mixin(create_fn!(mixin("`", i, "`"))); I think that's equivalent to `i.stringof` anyway.

Re: dlang.org/spec/function.html#pure-functions example

2023-10-13 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 12 October 2023 at 19:33:32 UTC, Paul wrote: If **int x** is global mutable state, what does static mutable state look like? In addition to Jonathan's reply, see: https://dlang.org/spec/function.html#local-static-variables

Re: how to assign multiple variables at once by unpacking array?

2023-10-08 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 7 October 2023 at 17:23:40 UTC, ryuukk_ wrote: there was a DIP for tuple/deconstruction prior to that question, sadly nothing came out of it, I don't think it was formally submitted. and now the language is frozen... The DIP process is temporarily suspended, it may be modifie

Re: C to D: please help translate this weird macro

2023-09-21 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven wrote: return cast(T*)(cast(void*)(cast(char*)ptr - __traits(getMember, T, member).offsetof))); There's a trailing `)` that needs removing. Also pretty sure it can be simplified to: return cast(T*)(cast(char

Re: C to D: please help translate this weird macro

2023-09-21 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 21 September 2023 at 02:57:07 UTC, Ki Rill wrote: On Thursday, 21 September 2023 at 02:23:32 UTC, Ki Rill wrote: wrote: [...] Translated it to this eventually: ```D auto nk_container_of(P, T)(P ptr, T type, const(char)* member) { return cast(T*)(cast(void*)(cast(char*)

Re: container vs standard array

2023-09-19 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 19 September 2023 at 19:57:34 UTC, Nick Treleaven wrote: This is because a single array can be passed to a typesafe variadic parameter rather than elements of that array type. And then immutable(char) doesn't convert to string. I think a non-variadic overload could be add

Re: container vs standard array

2023-09-19 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 19 September 2023 at 06:35:01 UTC, JG wrote: On Tuesday, 19 September 2023 at 00:34:01 UTC, vino wrote: //auto a = Array!string("Aname"); // throws error auto b = Array!char("Bname");// works auto c = Array!string("Aname", "Bname"); // works ... Looks

Re: Ideas to reduce error message size?

2023-08-31 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 30 August 2023 at 09:24:21 UTC, Paolo Invernizzi wrote: src/api3.d(49):called from here: `checkSql(Schema("public", src/dget/db.d(276): Error: template instance `api3.forgeSqlCheckerForSchema!(Schema("public", **BAZILLIONS of lines**> error instantiating ``` The comp

Re: toLower

2023-08-18 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 17 August 2023 at 09:28:05 UTC, Joel wrote: .map!(std.uni.toLower) .sort!"aonlineapp.d(8): Error: none of the overloads of template `std.algorithm.sorting.sort` are callable using argument types `!("a /dlang/dmd/linux/bin64/../../src/phobos/std/algor

Re: Why is GC.collect `pure`

2023-08-02 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 2 August 2023 at 17:55:12 UTC, Nick Treleaven wrote: On Wednesday, 2 August 2023 at 17:52:00 UTC, Nick Treleaven wrote: Now I'm wondering why those functions are marked `pure` - they must affect the GC's bookkeeping state. I guess it was because the GC's intern

Re: Why is GC.collect `pure`

2023-08-02 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 2 August 2023 at 17:52:00 UTC, Nick Treleaven wrote: Now I'm wondering why those functions are marked `pure` - they must affect the GC's bookkeeping state. Here's the pull that added it: https://github.com/dlang/druntime/pull/3561

Re: Why is GC.collect not @safe?

2023-08-02 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 2 August 2023 at 13:27:50 UTC, Steven Schveighoffer wrote: On 8/2/23 7:40 AM, Nick Treleaven wrote: Presumably an allocation like `new T` (for a type with a @safe constructor) can be made anywhere a call to `GC.collect` can be made, which may trigger a collection. So why isn&#

Re: How to free memory ater use of "new" to allocate it.

2023-07-17 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 16 July 2023 at 18:18:08 UTC, Alain De Vos wrote: 12 │ ~this(){ 13 │ writeln("Free heap"); 14 │ import object: destroy; 15 │ import core.memory: GC; 16 │ i=null; // But How to force GC free ? Firstly, be careful with class destr

Re: is ref inout redundant in: ref inout(T) opIndex(size_t index)

2023-06-21 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 19 June 2023 at 18:19:18 UTC, mw wrote: 2) `inout T` alone Steve covered everything, though you might also like to read the inout spec: https://dlang.org/spec/const3.html#inout

Re: Given an object, how to call an alias to a member function on it?

2023-05-03 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 3 May 2023 at 11:26:00 UTC, ag0aep6g wrote: On 03.05.23 13:13, Nick Treleaven wrote: void fun(alias method)(C c) {     void delegate() dg = &c.method;     dg(); } No, it doesn't. You're not using the alias. You're just accessing `c.method` directly. I

Re: Given an object, how to call an alias to a member function on it?

2023-05-03 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 2 May 2023 at 13:06:41 UTC, ag0aep6g wrote: void fun(alias method)(C c) { void delegate() dg; dg.funcptr = &method; dg.ptr = cast(void*) c; dg(); } This also works: void fun(alias method)(C c) { void delegate() dg = &c.method; dg(); }

Re: Why are globals set to tls by default? and why is fast code ugly by default?

2023-03-31 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 27 March 2023 at 08:44:41 UTC, wjoe wrote: e.g.: It used to be faster to ... - pre-calculate sin/cos tables, now the memory look up cost more cycles than the calculation itself ... - only redraw the parts of the screen that changed, now the branching is slower than to redraw eve

Re: Why are globals set to tls by default? and why is fast code ugly by default?

2023-03-31 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 26 March 2023 at 20:36:37 UTC, ryuukk_ wrote: Golang doesn't even have thread local storage, yet they do very well Go doesn't have a solution to preventing data races at compile time, they just say don't share memory. But what if you accidentally share memory? That is *very* easy t

Re: Why are globals set to tls by default? and why is fast code ugly by default?

2023-03-31 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 26 March 2023 at 20:39:21 UTC, ryuukk_ wrote: if my code doesn't do threads, why should i put my variable into TLS? I don't think writing __gshared is much of a burden. You can use -vtls to print out all variables that are TLS, and add that to an automated test to check you don't h

Re: Why are globals set to tls by default? and why is fast code ugly by default?

2023-03-31 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 26 March 2023 at 20:39:21 UTC, ryuukk_ wrote: if my code doesn't do threads, why should i put my variable into TLS? I don't think writing __gshared is a huge burden. You can use -vtls to print out all variables that are TLS, and add that to an automated test to check you don't have

Re: Why are globals set to tls by default? and why is fast code ugly by default?

2023-03-26 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 26 March 2023 at 18:07:03 UTC, ryuukk_ wrote: What i find even more weird is writing fast code is ugly in D Look at this ugly code ```D __gshared int fast_code_ugly; ``` Because it should be rare that __gshared is used. And if you need it, you won't be worried about how the storag

Re: templates and traits

2023-03-18 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 18 March 2023 at 19:22:07 UTC, Chris Katko wrote: ... So there's multiple sub-problems to solve. I asked this years ago, and got 90% of the way done and then lost the code and cannot find the original forum post. Maybe it was this?: https://forum.dlang.org/post/dqzxnctucwvyhstfz

Re: Is comparison of shared data thread-safe?

2023-03-16 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 16 March 2023 at 12:32:34 UTC, Nick Treleaven wrote: With -preview=nosharedaccess, I get: int y = 2; shared int x = y; // OK assert(x == 2); // no error y = x; // error This also does not error: ```d bool b = x == 3; ``` Filed: https://issues.dlang.org

Is comparison of shared data thread-safe?

2023-03-16 Thread Nick Treleaven via Digitalmars-d-learn
With -preview=nosharedaccess, I get: int y = 2; shared int x = y; // OK assert(x == 2); // no error y = x; // error So for the assignment to y, reading x is an error and atomicLoad should be used instead. But is it an oversight that reading x in the assert is not an error? I

Re: Non-ugly ways to implement a 'static' class or namespace?

2023-01-30 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear wrote: ``` final abstract class Algo { void drawLine(Canvas c, Pos from, Pos to) { .. }; } ``` This solution seems like a bit of a hack, which is why I don't like it. Interesting solution if you put `static:` in there. Alte

Re: Where I download Digital Mars C Preprocessor sppn.exe?

2023-01-23 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 23 January 2023 at 17:15:30 UTC, Nick Treleaven wrote: On Saturday, 2 April 2022 at 21:57:02 UTC, Marcone wrote: Where I download Digital Mars C Preprocessor sppn.exe? I need it to use ImportC Found this thread by googling `dlang sppn.exe`. For the record, it can be obtained from

Re: Where I download Digital Mars C Preprocessor sppn.exe?

2023-01-23 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 2 April 2022 at 21:57:02 UTC, Marcone wrote: Where I download Digital Mars C Preprocessor sppn.exe? I need it to use ImportC Found this thread by googling `dlang sppn.exe`. For the record, it can be obtained from sppn.zip here: http://ftp.digitalmars.com/ I didn't have it for so

Re: Preventing nested struct destructor accessing stack frame

2022-12-23 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 20 December 2022 at 06:31:09 UTC, ag0aep6g wrote: On 16.12.22 14:07, Nick Treleaven wrote: This seems to work:     ~this() @trusted { if (&i > cast(void*)1024) i++; } It would be better if there was a struct property to get the context pointer though. A quick test s

Re: Unique!struct bug - Re: unique_ptr | Unique for autoclose handle

2022-12-16 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 15 December 2022 at 20:12:12 UTC, Ali Çehreli wrote: I think this is a bug because the documentation clearly talks about destroying the object: OK: https://github.com/dlang/phobos/pull/8651 > do we need to do some kind of deprecation? The behavior is so different from the inten

Re: Preventing nested struct destructor accessing stack frame

2022-12-16 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 16 December 2022 at 12:17:40 UTC, Nick Treleaven wrote: It seems destroy clears the context pointer. Is there a way to test if the context pointer is null in the dtor, to prevent the increment? This seems to work: ~this() @trusted { if (&i > cast(void*)1024) i++

Preventing nested struct destructor accessing stack frame

2022-12-16 Thread Nick Treleaven via Digitalmars-d-learn
This code segfaults when the GC calls the dtor after the unittest succeeds: ```d unittest { int i; struct S { ~this() { i++; } } (*new S).destroy; } ``` It seems destroy clears the context pointer. Is there a way to test if the context pointer is null in the dtor, t

Re: pointer escaping return scope bug?

2022-12-15 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 15 December 2022 at 20:02:38 UTC, Nick Treleaven wrote: auto f() return @trusted => p ? p : v.ptr; Whoops, that can't be @trusted unless I `assert(p)`.

Re: pointer escaping return scope bug?

2022-12-15 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:24:33 UTC, Dukc wrote: On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: OK, so how do I make `lf` implicitly scope? Have the `int*` inside it to point to a local, or assign another `scope int*` to it. Thanks, this works: ```d @safe

Unique!struct bug - Re: unique_ptr | Unique for autoclose handle

2022-12-15 Thread Nick Treleaven via Digitalmars-d-learn
On Wednesday, 14 December 2022 at 17:41:07 UTC, Ali Çehreli wrote: I've never used Unique but I think it has a bug (or a design issue?): Its destructor is the following: ~this() { if (_p !is null) { destroy(_p); _p = null; } } Because

  1   2   >