Re: T[] opIndex() Error: .. signal 11

2023-10-04 Thread ag0aep6g via Digitalmars-d-learn
On 03.10.23 20:26, Paul Backus wrote: Naturally, this lowering is completely absent from [the language spec's section on `foreach`.][1] According to the spec, the only ways to iterate over a `struct` type are `opApply` and the input range interface. I think it would probably be less confusing

Re: Question about interface implementation

2023-05-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.05.23 12:55, Theo wrote: As for the other part, if I use an abstract base class, I *must* indicate when i'm overriding the base class method by explicately saying 'override'. I wouldn't mind if implementing interface methods required `override` as well. I don't know if there is a

Re: Question about interface implementation

2023-05-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.05.23 12:28, ag0aep6g wrote: Since @trusted functions are guaranteed (by the programmer) to be safe, they are allowed to overload/implement @safe functions/prototypes. *override

Re: Question about interface implementation

2023-05-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.05.23 11:55, Theo wrote: class MerchantShip : Ship {     private int speed = 0; // If only I had 'private(this)' !!     // how do I know this method is actually an implementation of an interface method     // and not a method specific to this class?     // AND ... how come I can

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

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

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

2023-05-02 Thread ag0aep6g via Digitalmars-d-learn
On 02.05.23 14:52, Quirin Schroll wrote: How do I invoke the member function in a reliable way? Given `obj` of the type of the object, I used `mixin("obj.", __traits(identifier, memberFunc), "(params)")`, but that has issues, among probably others, definitely with visibility. (The member

Re: Linking external functions?

2023-04-18 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 18 April 2023 at 20:05:05 UTC, DLearner wrote: Is the declaration inside main not visible to the linker? It affects the (fully qualified and mangled) name of the function. Compare: ```d extern(C) void ExtCallee(); pragma(msg, ExtCallee.mangleof); /* ExtCallee (correct name) */

Re: Linking external functions?

2023-04-18 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 18 April 2023 at 19:49:04 UTC, DLearner wrote: ``` void main() { import std.stdio; extern(C) void ExtCallee(); ``` Move that declaration out of main.

Re: The Phobos Put

2023-03-29 Thread ag0aep6g via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 19:49:47 UTC, Ali Çehreli wrote: I thought Salih was proposing two more overloads to the existing put(). When I copy the existing put(), which takes 'ref R', not R[], then the code does not compile: auto put(R)(R[] range, R[] source) => putImpl(range, source);

Re: The Phobos Put

2023-03-29 Thread ag0aep6g via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 16:44:31 UTC, Ali Çehreli wrote: On 3/29/23 09:27, Salih Dincer wrote: > In this way, > it could also be used directly with slices. For example: > auto put(R)(R[] range, R[] source) >=> putImpl(range, source); That's for rvalues. > auto put(R)(ref R[]

Re: read/peek and automatically advance index in buffer

2023-03-16 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 16 March 2023 at 18:39:00 UTC, jwatson-CO-edu wrote: ```d int rtnVal = buffer.peek(int,Endian.bigEndian)(); // Error: found `,` when expecting `.` following int ``` You just forgot the exclamation mark there.

Re: How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread ag0aep6g via Digitalmars-d-learn
On 11.03.23 14:22, rempas wrote: On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote: alias Foo(T : U*, U) = Foo!U; alias Foo(T) = T; static assert(is(Foo!(int*) == int)); static assert(is(Foo!(int**) == int)); static assert(is(Foo!(int***) == int)); static assert(is(Foo!(int) ==

Re: How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread ag0aep6g via Digitalmars-d-learn
On 11.03.23 13:49, rempas wrote: but what about pointers to pointers like: `int`? Is there a way that I would be able to always get the scalar type of a pointer regardless of how many levels it is? As always, I'm searching for a solution that will work in `BetterC`. alias Foo(T : U*, U)

Re: Transform static immutable string array to tuple.

2023-02-19 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 19 February 2023 at 11:08:34 UTC, realhet wrote: Is there a better way to transform a string array to a tuple or to an AliasSeq? ``` mixin(customSyntaxPrefixes.format!`tuple(%(%s,%))`) ``` https://dlang.org/phobos/std_meta.html#aliasSeqOf

Re: Why does this code only work with `std.conv.to` whilst not with `cast`?

2023-01-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.01.23 12:29, thebluepandabear wrote: ```D interface ICustomDrawable {     void render(sfRenderWindow* renderWindow); } [...]> class Button : ICustomDrawable { [...] } [...] class StackLayout : ICustomDrawable { [...]     ICustomDrawable[] _children; } ``` For some reason, when I

Re: Preventing nested struct destructor accessing stack frame

2022-12-19 Thread ag0aep6g via Digitalmars-d-learn
On 16.12.22 14:07, Nick Treleaven wrote: This seems to work:     ~this() @trusted { if ( > cast(void*)1024) i++; } It would be better if there was a struct property to get the context pointer though. A quick test suggests that the context pointer is the last item in `tupleof`. So this

Re: How is this code invalid?

2022-12-16 Thread ag0aep6g via Digitalmars-d-learn
On Saturday, 17 December 2022 at 00:23:32 UTC, thebluepandabear wrote: ```D int[] numbersForLaterUse; void foo(int[] numbers...) { numbersForLaterUse = numbers; } struct S { string[] namesForLaterUse; void foo(string[] names...) { namesForLaterUse = names; } } ``` [...] The

Re: printf, writeln, writefln

2022-12-06 Thread ag0aep6g via Digitalmars-d-learn
On 07.12.22 01:35, ryuukk_ wrote: On Tuesday, 6 December 2022 at 23:41:09 UTC, H. S. Teoh wrote: [...] In D, strings are not the same as char*.  You should use std.conv.fromStringZ to convert the C char* to a D string. [...] no, you don't "need" to use std conv Here is an alternative that

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-05 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 5 December 2022 at 15:08:41 UTC, rempas wrote: Got it! I guess they could also just allow us to use bracket notation to do the same thing. So something like: ```d foreach (i; 0 .. list.length) { (cast(int*)ptr[i]) = i; } ``` This is what happens with arrays anyways. And arrays

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-04 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 4 December 2022 at 16:33:35 UTC, rempas wrote: First a little bit of theory. A pointer just points to a memory address which is a number. So when I add "10" to this pointer, it will point ten bytes after the place it was pointing to, right? Not quite. Adding 10 to a T* means

Re: Why can't D store all UTF-8 code units in char type? (not really understanding explanation)

2022-12-03 Thread ag0aep6g via Digitalmars-d-learn
On 02.12.22 22:39, thebluepandabear wrote: Hm, that specifically might not be. The thing is, I thought a UTF-8 code unit can store 1-4 bytes for each character, so how is it right to say that `char` is a utf-8 code unit, it seems like it's just an ASCII code unit. You're simply not using the

Re: Can't assign extern(C) function pointer to D variable?

2022-11-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.11.22 22:11, XavierAP wrote: I was surprised when it didn't compile, though I immediately found it understandable... Already read through https://dlang.org/spec/interfaceToC.html and https://wiki.dlang.org/Bind_D_to_C Is it really the case (that an extern(C) function pointer cannot be

Re: pointer escaping return scope bug?

2022-11-19 Thread ag0aep6g via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: OK, so how do I make `lf` implicitly scope? By explicit/implicit I just meant this: scope explicit = new int; int x; auto implicit = That's probably not helping with whatever you want to accomplish.

Re: pointer escaping return scope bug?

2022-11-19 Thread ag0aep6g via Digitalmars-d-learn
On 19.11.22 15:07, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile {     private int* fps;     auto fp() return scope => fps; } void main() {     int* p;     {     auto lf = LockedFile(new int);  

Re: Removing an element from an array

2022-11-11 Thread ag0aep6g via Digitalmars-d-learn
On Friday, 11 November 2022 at 06:10:33 UTC, Alexander Zhirov wrote: On Friday, 11 November 2022 at 05:36:37 UTC, Alexander Zhirov wrote: On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov wrote: ```d import std.algorithm; arr = arr.remove(arr.countUntil(fragment)); ``` And will

Re: DConf '22: No-Allocated 0-terminated path strings

2022-10-21 Thread ag0aep6g via Digitalmars-d-learn
On Friday, 21 October 2022 at 13:49:01 UTC, cc wrote: ```d //version=AllowMalloc; auto toCStringThen(alias dg, Range)(Range src) /*nothrow*/ if (isInputRange!Range && !isInfinite!Range) { ``` [...] May need to be cleaned up for character types and needs to iterate twice if allocations are

Re: Find out what type my class is being converted to for comparisons

2022-10-18 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 18 October 2022 at 18:53:41 UTC, Matthew Rushworth wrote: The entirety of opEquals: ``` /// both matrices have to have an identical shape to compare /// each element must be identical to match bool opEquals(size_t X, size_t Y)(const Matrix!(X,Y) m1, const Matrix!(X,Y) m2) { for

Re: Example for multi level template composition

2022-10-10 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 10 October 2022 at 06:30:05 UTC, Arun wrote: Stumbled upon this question on HN https://news.ycombinator.com/item?id=33142751#33147401 Can I write template A and then apply it to itself to get template B and then apply that onto template C to get template D. Does anyone have an

Re: Doubt about char.min/max == typeid(char)

2022-10-07 Thread ag0aep6g via Digitalmars-d-learn
On 07.10.22 07:06, bauss wrote: If you don't need to assign it then you can cast it. ``` void a(int x) { ... } a(cast(int)char.max); ``` Even though in the example above the cast isn't necessary, if you want to be sure a(int) is called then you must cast it, since an overload of char will

Re: to delete the '\0' characters

2022-09-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.09.22 13:14, ag0aep6g wrote: On 22.09.22 12:53, Salih Dincer wrote: [...] ```d auto foo(string s) {    string r;    foreach(c; s)    { if(c > 0) {    r ~= c; }    }    return r; } ``` [...] Here's a snippet that's a bit shorter than yours and doesn't copy the data:

Re: to delete the '\0' characters

2022-09-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.09.22 12:53, Salih Dincer wrote: Is there a more accurate way to delete the '\0' characters at the end of the string? I tried functions in this module: https://dlang.org/phobos/std_string.html ```d auto foo(string s) {   string r;   foreach(c; s)   {     if(c > 0)     {   r ~=

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 29 August 2022 at 16:21:53 UTC, ag0aep6g wrote: You never change `pageId`. So as far as I can tell, you're always `seek`-ing to the same position, and you just overwrite the same piece of the file again and again. Whoops. I guess I missed the point of the question there.

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 28 August 2022 at 22:46:17 UTC, Gavin Ray wrote: I've put the code, stripped to a minimal example here: - https://ldc.godbolt.org/z/fzsx3Tnnn [...] But if the same code is placed inside of a `for` loop, suddenly no writes occur: [...] Does anyone know what is happening here?

Re: Casting rules

2022-08-26 Thread ag0aep6g via Digitalmars-d-learn
On Friday, 26 August 2022 at 20:42:07 UTC, JG wrote: Where can I find rules about casting. e.g. I assume casting away immutable is undefined behavior (or implementation defined behavior). What about casting to immutable (I would expect at most it only to be allowed if your type has no

Re: Index an AliasSeq with a run-time index

2022-08-14 Thread ag0aep6g via Digitalmars-d-learn
On 14.08.22 12:08, Per Nordlöw wrote: How do I index an `AliasSeq` with an integer known at run-time? With a `switch` that has a `case` for every possible index: import std.meta: AliasSeq; alias seq = AliasSeq!("foo", "bar", "baz"); string f(size_t rti) { sw: switch (rti) {

Re: Cast converts AA to rvalue?

2022-08-10 Thread ag0aep6g via Digitalmars-d-learn
On Wednesday, 10 August 2022 at 17:14:08 UTC, Johan wrote: ``` void f() { aa[1] = 1; // error } shared static this() { f(); } ``` I had considered it, but discarded it... `f` is also a template in our code. Your remark made me check again, and the call chain is short, perhaps I'll

Re: Cast converts AA to rvalue?

2022-08-10 Thread ag0aep6g via Digitalmars-d-learn
On 10.08.22 10:20, Johan wrote: ``` shared immutable int[int] aa; void main () {     // (cast()aa)[1] = 1; // works without immutable     (*cast(int[int]*)())[1] = 1; } ``` We have shared static constructors for that: shared static this() { aa[1] = 1; /* no cast needed */ }

Re: Acess variable that was set by thread

2022-08-08 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 8 August 2022 at 20:36:34 UTC, Steven Schveighoffer wrote: [...] shared gives you a sense that the language is helping you prevent problems. Again, if C isn't playing ball, this is a lie. The C side is playing ball, by whatever rules the C library chooses. `shared` (with

Re: Acess variable that was set by thread

2022-08-08 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 8 August 2022 at 19:33:14 UTC, Steven Schveighoffer wrote: There's nothing clever. If you want to access C globals, you should use `__gshared`, because that's what it is. Using `shared`, isn't going to save you at all. Yes, using `shared` does save you. C might not have a `shared`

Re: Acess variable that was set by thread

2022-08-08 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 8 August 2022 at 17:45:03 UTC, bauss wrote: On Monday, 8 August 2022 at 13:55:02 UTC, ag0aep6g wrote: auto x = s.x; ``` Your problem is here and not because it was __gshared. You're copying the value and obviously it can be changed in the meantime, that's common sense.

Re: Acess variable that was set by thread

2022-08-08 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 8 August 2022 at 14:29:43 UTC, Steven Schveighoffer wrote: C has no notion of shared, so it's not the right type. Putting `shared` on it is kind of lying, and can lead to trouble. Better to be explicit about what it is. Nonsense. Putting `shared` on a shared variable is not

Re: Acess variable that was set by thread

2022-08-08 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 8 August 2022 at 13:31:04 UTC, Steven Schveighoffer wrote: On 8/8/22 6:17 AM, ag0aep6g wrote: [...] Never ever use `__gshared` ever. It's a glaring safety hole. Use `shared` instead. If you are interfacing with C, you need __gshared. But yeah, you should use shared in this case.

Re: Acess variable that was set by thread

2022-08-08 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 8 August 2022 at 12:45:20 UTC, bauss wrote: On Monday, 8 August 2022 at 10:17:57 UTC, ag0aep6g wrote: Never ever use `__gshared` ever. [...] To sum it up: Single-write/Single-read? __gshared Single-write/Multi-read? __gshared Multi-write/Single-read? shared

Re: Acess variable that was set by thread

2022-08-08 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 8 August 2022 at 07:14:33 UTC, vc wrote: it seems change it to working is working ```d __gshared bool zeus; ``` but as I'm new in to D, i will like to hear thoughts even if it works for me Never ever use `__gshared` ever. It's a glaring safety hole. Use `shared` instead. If

Re: Ranges

2022-08-07 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 7 August 2022 at 15:34:19 UTC, pascal111 wrote: Everyone knows that slices are not pointers that pointers are real work, but slices are like a simple un-deep technique that is appropriate for beginners, but after that in advanced level in programming, we should use pointers to do

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 15:52:34 UTC, pascal111 wrote: But how can we accept that both are functions and expressions at the same time and we know that expressions can be used to build functions themselves?!! I think they are not the same. This is getting ridiculous. I'm out.

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 15:31:51 UTC, pascal111 wrote: Surely, because it seems that you are real man, your word must be taken. Isn't `(foo) { return bar; }` an anonymous function or am I a wrong?!! It IS a function, not an expression. It's both an anonymous function and an expression. I

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:52:03 UTC, pascal111 wrote: If `foo => bar` == `(foo) { return bar; }`, then `foo => bar` is a function. "=>" is not an operator, it's a special symbol for lambda "function". If A == B, so A's types is the same of B's type. How can it be withstanding `foo =>

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:39:17 UTC, pascal111 wrote: On Monday, 1 August 2022 at 14:34:45 UTC, ag0aep6g wrote: [...] `a => a > 0` is not a statement. It's an expression. But it is still a "function", and functions make statements!! It's not a normal expression. It's a normal

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote: Many of D rules are taken from C, we know that, so a general basic rule is to put ";" after each statement, so the previous statement of filter should be "auto r = chain(a, b).filter!(a => a > 0;);"? Why D leaves ";" in this case? `a

Re: Is this a violation of const?

2022-07-30 Thread ag0aep6g via Digitalmars-d-learn
On 30.07.22 09:15, Salih Dincer wrote: It's possible to do this because it's immutable.  You don't need an extra update() function anyway. ```d void main() {     auto s = S("test A");     s.update = (_) { s.s = _; };     s.update("test B");     assert(s.s == "test B");     s.s = "test

Re: Is this a violation of const?

2022-07-30 Thread ag0aep6g via Digitalmars-d-learn
On 29.07.22 23:56, Andrey Zherikov wrote: In the example below `func` changes its `const*` argument. Does this violates D's constness? Yes. Here's a modified example to show that you can also violate `immutable` this way: struct S { string s; void delegate(string s) @safe update; }

Re: char* pointers between C and D

2022-07-25 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 25 July 2022 at 11:14:56 UTC, pascal111 wrote: module main; import std.stdio; import core.stdc.stdio; import core.stdc.string; int main(string[] args) { const(char)[] ch1 = "Hello World!"; char[] ch2="Hello World!".dup; const(char) *p1; char *p2;

Re: Working with arrays (flatten, transpose, verfify rectangular)

2022-07-20 Thread ag0aep6g via Digitalmars-d-learn
On 20.07.22 11:18, anonymouse wrote:     d     auto isRectangular(A)(A a) if (isArray!A)     {     bool result;     size_t length = a[0].length;     foreach (e; a) {     result = e.length == length;     }     return

Re: null == "" is true?

2022-07-12 Thread ag0aep6g via Digitalmars-d-learn
On 12.07.22 22:14, H. S. Teoh wrote: Pedantically, no, they're not the same. You can assign null to a pointer, but you can't assign [] to a pointer. `null` is a supertype of `[]`. But probably nobody actually cares about this distinction. :-D If we're ignoring context, "null" has four

Re: null == "" is true?

2022-07-12 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 19:02:01 UTC, user1234 wrote: On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: [...] Do not rely on this, however; Absolutely. I'd like to add: especially as default parameter value that's an array. Never use null. use `[]` (empty array literal).

Re: How can I match every instance of a template type (struct)?

2022-07-12 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 13:56:11 UTC, rempas wrote: On Tuesday, 12 July 2022 at 13:37:49 UTC, ag0aep6g wrote: static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!\n"); } Haah? Ok, what does this work anyway? I thought you needed parenthesis for more than 1 templated

Re: How can I match every instance of a template type (struct)?

2022-07-12 Thread ag0aep6g via Digitalmars-d-learn
On 12.07.22 15:34, rempas wrote: extern (C) void main() {   auto obj = make_test(20);   static if (is(typeof(obj) == Test)) { printf("YES!!!\n"); } } static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!\n"); }

Re: Cannot copy void[] to void[] in @safe code?

2022-07-08 Thread ag0aep6g via Digitalmars-d-learn
On Friday, 8 July 2022 at 10:58:24 UTC, wjoe wrote: My understanding is that a void[] doesn't have a distinct type but since the length is bytes and not elements this makes me believe that under the hood they are byte arrays - or, rather, managed chunks of memory. How's copying memory without

Re: DIP1000

2022-06-23 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 23 June 2022 at 20:27:44 UTC, Ola Fosheim Grøstad wrote: On Thursday, 23 June 2022 at 19:38:12 UTC, ag0aep6g wrote: ```d void connect(ref scope node a, return scope node* b) ``` Thanks, so the `return scope` means «allow escape», not necessarily return? It means "may be

Re: DIP1000

2022-06-23 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 23 June 2022 at 16:08:01 UTC, Ola Fosheim Grøstad wrote: How am I supposed to write this: ```d import std; @safe: struct node { node* next; } auto connect(scope node* a, scope node* b) { a.next = b; } void main() { node x; node y; connect(,); } ``` Error:

Re: want to confirm: gc will not free a non-gc-allocated field of a gc-allocated object?

2022-06-07 Thread ag0aep6g via Digitalmars-d-learn
On 07.06.22 11:00, max haughton wrote: On Tuesday, 7 June 2022 at 08:56:29 UTC, ag0aep6g wrote: [...] That wasn't mw's question. I also answered this in my original one IIRC. You didn't.

Re: want to confirm: gc will not free a non-gc-allocated field of a gc-allocated object?

2022-06-07 Thread ag0aep6g via Digitalmars-d-learn
On 07.06.22 03:02, max haughton wrote: I'm talking about the data in the array. void[] might contain pointers, float[] does not so it won't be scanned. That wasn't mw's question.

Re: want to confirm: gc will not free a non-gc-allocated field of a gc-allocated object?

2022-06-06 Thread ag0aep6g via Digitalmars-d-learn
On 07.06.22 00:22, max haughton wrote: float[] doesn't contain pointers, so the GC won't do anything to or with it. wat float[] is a pointer (plus a length). The GC will deal with it like any other pointer.

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread ag0aep6g via Digitalmars-d-learn
On 19.05.22 12:15, Chris Katko wrote: given ```D struct COLOR { float r, g, b, a; // a is alpha (opposite of transparency) } auto red   = COLOR(1,0,0,1); auto green = COLOR(0,1,0,1); auto blue  = COLOR(0,0,1,1); auto white = COLOR(1,1,1,1); //etc ``` is there a way to do: ```D auto myColor =

Re: Creating a custom iota()

2022-05-12 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 12 May 2022 at 17:06:39 UTC, Ali Çehreli wrote: void main() { const st = DateTime(Duration(0)); [...] // (0) I think D should not insist on 'const' // when copying types that have no indirections. // We shouldn't need the cast() below in this case. [...]

Re: Trait for "can be instantiated"?

2022-05-09 Thread ag0aep6g via Digitalmars-d-learn
On 09.05.22 23:24, Ben Jones wrote: enum x; enum y = 5; struct Wrap(alias T) {     static if(isType!T){ T value; //When t == x this doesn't work because `x` is opaque and has no default initializer     } } [...] x is a "type" as far as isType is concerned (which makes sense to

Re: Parameters declared as the alias of a template won't accept the arguments of the same type.

2022-05-02 Thread ag0aep6g via Digitalmars-d-learn
On 02.05.22 22:47, Stanislav Blinov wrote: On Monday, 2 May 2022 at 20:16:04 UTC, ag0aep6g wrote: On 02.05.22 21:17, Stanislav Blinov wrote: On Monday, 2 May 2022 at 16:29:05 UTC, Loara wrote: [...] ```d     template MyAlias(T){   alias MyAlias = int;     }     T simp(T)(MyAlias!T val){

Re: Parameters declared as the alias of a template won't accept the arguments of the same type.

2022-05-02 Thread ag0aep6g via Digitalmars-d-learn
On 02.05.22 21:17, Stanislav Blinov wrote: On Monday, 2 May 2022 at 16:29:05 UTC, Loara wrote: [...] ```d     template MyAlias(T){   alias MyAlias = int;     }     T simp(T)(MyAlias!T val){   return T.init;     }     int main(){   simp(3);//Impossible to deduce T Why? That's

Re: stack frame & dangling pointer weirdness

2022-04-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.04.22 13:25, Alain De Vos wrote: How can i force an error to be thrown when doing something "bad" ? Use @safe.

Re: stack frame & dangling pointer weirdness

2022-04-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.04.22 07:49, Alain De Vos wrote: int *p=null; void myfun(){ int x=2; p= writeln(p); writeln(x); } myfun(); *p=16; writeln(p); writeln(*p); `p` is no longer valid after `myfun` returns. Dereferencing it is an error. The two `writeln` calls in `main` re-use the memory

Re: Why do immutable variables need reference counting?

2022-04-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.04.22 15:27, H. S. Teoh wrote: On Sun, Apr 17, 2022 at 01:06:36PM +, wjoe via Digitalmars-d-learn wrote: [...] On the matter of undefined behavior. Technically a program is in undefined behavior land after throwing an error, thus every unittest that continues after assertThrown is

Re: Why do immutable variables need reference counting?

2022-04-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.04.22 15:06, wjoe wrote: On the matter of undefined behavior. Technically a program is in undefined behavior land after throwing an error, thus every unittest that continues after assertThrown is therefore nonsense code, is it not ? Yes. Failing asserts are a messy part of the

Re: Why do immutable variables need reference counting?

2022-04-14 Thread ag0aep6g via Digitalmars-d-learn
On 14.04.22 13:42, wjoe wrote: Undefined behavior yes, but regardless the example proves it can be done in @system code. A few versions ago, possibly due to a bug or regression, the compiler didn't complain in @safe code either. Of course you are correct academically. However, since it's

Re: Why do immutable variables need reference counting?

2022-04-12 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 12 April 2022 at 19:54:13 UTC, wjoe wrote: Especially since it's only a promise and the compiler accepts this: void foo (const(char)[] arr) { cast(char[])arr[0..3] = "baz"; } string bar = "123"; foo(bar); assert(bar=="baz"); But I could cast away const and modify the string bar.

Re: How to remove all characters from a string, except the integers?

2022-03-04 Thread ag0aep6g via Digitalmars-d-learn
On Friday, 4 March 2022 at 19:51:44 UTC, matheus wrote: import std.datetime.stopwatch; import std.stdio: write, writeln, writef, writefln; import std; void printStrTim(string s,StopWatch sw){ writeln("\nstr: ", s ,"\nTim(ms): ", sw.peek.total!"msecs" ,"\nTim(us): ",

Re: split Error - no overload matches

2022-02-14 Thread ag0aep6g via Digitalmars-d-learn
On 14.02.22 12:14, forkit wrote: However, if I uncomment the //import std.uni : isWhite; then it will compile. I don't understand. I thought 'import std;' would be sufficient here?? "isWhite" is ambiguous. There's std.uni.isWhite and std.ascii.isWhite. `import std;` can't know which one you

Re: how to handle very large array?

2022-02-09 Thread ag0aep6g via Digitalmars-d-learn
On 09.02.22 11:09, MichaelBi wrote: On Wednesday, 9 February 2022 at 10:05:23 UTC, MichaelBi wrote: [...] got outofmemory error: core.exception.OutOfMemoryError@src\core\lifetime.d(126): Memory allocation failed https://adventofcode.com/2021/day/6#part2 "Suppose the lanternfish live

Re: How to print unicode characters (no library)?

2021-12-27 Thread ag0aep6g via Digitalmars-d-learn
On 27.12.21 15:23, Adam D Ruppe wrote: Let's look at: "Hello \n"; [...] Finally, there's "string", which is utf-8, meaning each element is 8 bits, but again, there is a buffer you need to build up to get the code points you feed into that VM. [...] H, e, l, l, o, , MORE elements>, , ,

Re: Double bracket "{{" for scoping static foreach is no longer part of D

2021-12-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.12.21 17:01, rikki cattermole wrote: Anyway, AliasAssign has nothing to do with this. This "trick" creates a closure aka ``() { ... }``. Thats all its doing. From the AST dump: ``` import object; import std; void main() { {     string str = "Abc";     writeln("Hello D ",

Re: A debug class has started

2021-12-13 Thread ag0aep6g via Digitalmars-d-learn
On 13.12.21 12:09, drug wrote: That's because `str` is initialized by a literal and you can not change it by definition. When you call `toStringz` it duplicates that literal (adding terminating zero at the end) and the duplicate is mutable. I would recommend do not use `toStringz` and just

Re: How to test if a string is pointing into read-only memory?

2021-10-12 Thread ag0aep6g via Digitalmars-d-learn
On 12.10.21 10:19, jfondren wrote: ```d /+ Unfortunately, this isn't reliable.  We could make this work if string literals are put  in read-only memory and we test if s[] is pointing into  that.  /* Peek past end of s[], if it's 0, no conversion necessary.  * Note that the compiler will

Re: Is this a compiler aliasing bug?

2021-09-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.09.21 11:44, Chris Katko wrote: bool is_colliding_with(drawable_object_t obj) //was a class member { [...] alias x2 = obj.x; alias y2 = obj.y; alias w2 = obj.w; alias h2 = obj.h; [...]     } Those aliases don't work like you want them to. You can't have

Re: Merge 2 structs together (into a single struct)?

2021-09-16 Thread ag0aep6g via Digitalmars-d-learn
On 16.09.21 22:53, jfondren wrote: string joinstruct(A, B)(string name) {     string s = "struct " ~ name ~ " {";     alias memA = __traits(allMembers, A);     alias memB = __traits(allMembers, B);     alias initA = A.init.tupleof;     alias initB = B.init.tupleof;     static foreach (i; 0

Re: Question on Immutability

2021-08-30 Thread ag0aep6g via Digitalmars-d-learn
On 31.08.21 02:50, Mike Parker wrote: Member functions marked as immutable can be called on both mutable and immutable instances. That's not true.

Re: compile time compression for associatve array literal

2021-08-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.08.21 08:14, Brian Tiffin wrote: From ~~a~~ little reading, it seems associative array literal initialization is still pending for global scope, but allowed in a module constructor?  *If I understood the skimming surface reading so far*. ```d immutable string[string] things; static

Re: Union member positions?

2021-08-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.08.21 15:46, z wrote: Is it possible to set a "position" on a union member? or is there is a language-integrated equivalent? For example, to get access to each byte in an unsigned integer while still supporting the original type. ```D ///a single uint that would be accessed as two

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int v); //

Re: compare types of functions.

2021-08-04 Thread ag0aep6g via Digitalmars-d-learn
On 02.08.21 22:14, vit wrote: Why this doesn't work: ```d template DestructorType(T){ alias Get(T) = T;     alias DestructorType = Get!(typeof((void*){     T tmp;     })); } struct Foo{     ~this()@safe{} } ``` ```d void main(){     //Error: static assert:  `is(void

Re: Creating immutable arrays in @safe code

2021-07-18 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 15:56, ag0aep6g wrote: At a glance, the only meaningful use of `PURE.strong` seems to be in dcast.d, introduced by the PR you linked. Changing that to `PURE.const_` doesn't break any tests for me. So I'm inclined to believe that `PURE.strong` is nonsense, and that `PURE.const_`

Re: Creating immutable arrays in @safe code

2021-07-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 14:56, Dennis wrote: On Saturday, 17 July 2021 at 12:05:44 UTC, ag0aep6g wrote: Hm, as far as I understand, "strongly pure" doesn't require `immutable` parameters. `const` should be enough. The spec says: "A strongly pure function has no parameters with mutable indirections" [1].

Re: Creating immutable arrays in @safe code

2021-07-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 13:05, Dennis wrote: There used to be a complex `isReturnIsolated` check, but the [fix for issue 15660](https://github.com/dlang/dmd/pull/8048) reduced it to a check 'is the function strongly `pure`' which means 'parameters are values or immutable'. To reduce code breakage, the

Re: Creating immutable arrays in @safe code

2021-07-16 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 00:27, H. S. Teoh wrote: Hmm, OK. Not sure why .array isn't being inferred as unique... but yeah, you probably have to resort to using @trusted with .assumeUnique. In addition to `pure`, you also need a const/immutable input and a mutable output, so that the output cannot be a

Re: mixin template's alias parameter ... ignored ?

2021-07-12 Thread ag0aep6g via Digitalmars-d-learn
On 13.07.21 03:03, someone wrote: On Monday, 12 July 2021 at 23:28:29 UTC, ag0aep6g wrote: [...] I'm not sure where we stand with `in` You mean *we* = D developers ? Yes. Let me rephrase and elaborate: I'm not sure what the current status of `in` is. It used to mean `const scope`. But

Re: mixin template's alias parameter ... ignored ?

2021-07-12 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 12 July 2021 at 22:35:27 UTC, someone wrote: On Monday, 12 July 2021 at 05:33:22 UTC, ag0aep6g wrote: [...] Teach me please: if I declare a variable right after the function declaration like this one ... ain't scope its default visibility ? I understand (not quite sure whether

Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread ag0aep6g via Digitalmars-d-learn
On 12.07.21 03:37, someone wrote: I ended up with the following (as usual advice/suggestions welcomed): [...]> alias stringUTF16 = dstring; /// same as immutable(dchar)[];> alias stringUTF32 = wstring; /// same as immutable(wchar)[]; Bug: You mixed up `wstring` and `dstring`. `wstring` is

Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.06.21 09:02, Mike Parker wrote: On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote: Ha! great. I didn't know `~` works for both single elements and array! `~` by itself is the concatenation operator and only works with two array operands. `~=` is the append operator and can append

Re: @trusted methods

2021-06-18 Thread ag0aep6g via Digitalmars-d-learn
On 18.06.21 14:40, vit wrote: Are asserts enough to make method @trusted or is something like throw exception or return error code necessary? Asserts are a debugging feature. They're not suitable to ensure safety, because they're simply skipped in release mode. `assert(false);` is the

Re: In general, who should do more work: popFront or front?

2021-06-14 Thread ag0aep6g via Digitalmars-d-learn
On 15.06.21 07:17, mw wrote: https://dlang.org/library/std/range/primitives/front.html the 2nd decl: dchar front(T) (   scope const(T)[] a ) pure @property @safe if (isAutodecodableString!(T[])); you can see `const` but https://dlang.org/library/std/range/primitives/pop_front.html void

Re: cannot take address of scope local in safe function

2021-06-14 Thread ag0aep6g via Digitalmars-d-learn
On 13.06.21 19:49, vit wrote: Is possible create and use scope output range allocated on stack in @safe code? Example: ```d //-dip1000     struct OutputRange{     private bool valid = true;     private void* ptr;     int count = 0;     void put(Val)(auto ref scope Val

  1   2   3   4   5   6   7   8   >