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

Re: cannot take address of scope local in safe function

2021-06-13 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 13 June 2021 at 16:27:18 UTC, vit wrote: Why I can take address of Foo variable but not Bar? ```d //-dip1000 struct Foo{ private double d; } struct Bar{ private void* ptr; } void main()@safe{ ///this is OK: { scope Foo x; scope ptr = }

Re: Cast class reference to pointer of another class?

2021-05-29 Thread ag0aep6g via Digitalmars-d-learn
On Saturday, 29 May 2021 at 21:01:14 UTC, JN wrote: this code compiles. Why? What is even the result in "f" in this case? On Saturday, 29 May 2021 at 21:03:12 UTC, JN wrote: fixed formatting: ```d struct Foo { } class Bar { } void main() { Bar b = new Bar(); Foo* f = cast(Foo*)b; }

Re: stack out of scope ?

2021-05-16 Thread ag0aep6g via Digitalmars-d-learn
On 16.05.21 19:24, Alain De Vos wrote: On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote: [...] Though I believe if you compile with -dip25 -dip1000 the compiler should emit an error for the above code.  If not, please file a bug against -dip1000. T I use ldc2. No dip flags here.

Re: ref struct member function

2021-05-14 Thread ag0aep6g via Digitalmars-d-learn
On 14.05.21 12:00, PinDPlugga wrote: Hi thank you both for your answers. I had understood from an earlier chapter how this could introduce a bug, but I was confused because the warning suggests attaching ```return``` to the parameter, which is empty in the declaration. `this` is considered a

Re: What's a good approach to DRY with the block code of a case-statement?

2021-04-26 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 26 April 2021 at 20:39:55 UTC, Jack wrote: I have a block of code that the only thing that change is the type passed in one of the template functions called so I'd like to make a DRY for this. But I'm not just replacing by a function due to control-flow, for example, there are

Re: Extern/scope issue

2021-04-03 Thread ag0aep6g via Digitalmars-d-learn
On 03.04.21 15:34, DLearner wrote: The following produces the expected result. However, changing extern(C) to extern(D) causes linker failures. To me, that is bizarre. Testmain: extern(C) int xvar; [...] Testmod: extern extern(C) int xvar; With `extern (C)`, those two `xvar`s refer to the

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:53, Steven Schveighoffer wrote: Maybe, but I wasn't responding to that, just your statement not to recommend -boundscheck=off. In any case, it wouldn't hurt, right? Right.

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:36, Steven Schveighoffer wrote: On 4/1/21 3:27 PM, ag0aep6g wrote: On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` [...] Yes, but you can recommend `-boundscheck=safeonly`, which

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` Please don't recommend `-boundscheck=off` to newbies. It's not just an optimization. It breaks @safe. If you want to do welding without eye

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values between 350-380 ms, and 81ms in Python. [...] ``` for

Re: Implicit conversion of unique chars[] to string

2021-03-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.03.21 02:07, Steven Schveighoffer wrote: const(char)[] x = "foo"; string chained = chainPath(x, "bar").array; Error: cannot implicitly convert expression array(chainPath(x, "bar")) of type const(char)[] to string And the answer is complex. You can't accept a const range, because they

Re: Implicit conversion of unique chars[] to string

2021-03-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.03.21 21:38, Per Nordlöw wrote: Am I the only one being annoyed by the fact that     chainPath(...).array doesn't implicit convert to string despite the array returned from .array is allocated by the GC. Works for me: import std.array: array; import std.path: chainPath; void

Re: question about ref keyword

2021-03-18 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 18 March 2021 at 16:59:24 UTC, Jack wrote: let's assume this class: class C { private S m_s; this() { m_s = S(30); } ref S value() { return m_s; } ref S value(ref S s) { return m_s = s;

Re: Compiler module import graph

2021-03-13 Thread ag0aep6g via Digitalmars-d-learn
On 13.03.21 15:20, frame wrote: Is there a tool to view module import graph? The compiler verbose output shows that the module is imported/parsed but not why. I wan't to avoid unnecessary imports to speed up compile times or avoid issues if the module contents are not fully compatible with

Re: unittest compiles w/o error though module file is not named after the module

2021-02-06 Thread ag0aep6g via Digitalmars-d-learn
On 06.02.21 16:05, kdevel wrote: On Saturday, 6 February 2021 at 14:52:57 UTC, Adam D. Ruppe wrote: [...] That one `import p;` is kinda weird, it should probably complain then you imported one thing and got another, but generally the name not matching is no problem at all. ```main.d

Re: Type of string literal concatenated with non-immutable char array

2021-02-01 Thread ag0aep6g via Digitalmars-d-learn
On 31.01.21 22:48, Per Nordlöw wrote: Why isn't     "Name " ~ name ~ " could not be found" implicitly convertible to `string`? If concatenation is guaranteed to allocate a new array, then it should be "strongly pure", and the conversion should work. I'm not sure if it is guaranteed to

Re: Renamed but non-selective import?

2021-01-12 Thread ag0aep6g via Digitalmars-d-learn
On 12.01.21 21:09, cc wrote: import core.sys.windows.windows; import mymodule; // contains a struct named MSG Error: `core.sys.windows.winuser.MSG` ... conflicts with `mymodule.MSG` vs import core.sys.windows.windows : winMSG = MSG; // this leaves out other symbols Error: undefined

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:51:04 UTC, Jack wrote: Here's what I'm trying to make to work: import std.container : SList; class C { static immutable Foo = new C(); // } alias Callback = void function(const C, int); void main() { auto l = SList!Callback(); auto a = (C

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:12:17 UTC, Jack wrote: thanks! now, how would I add const here? import std.container : SList; auto l = SList!Callabck(); doesn't work: auto l = SList!(const(Callabck())); auto l = SList!(const Callabck()); You said you want the callbacks to accept both

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 16:40:01 UTC, Jack wrote: let's say a I have this: void f(X foo) { } but I'd like to make f() accept immutable X too so instead of cast away everywhere in the code where immutable(X) is passed to f() or make a overload for this, are there any way to accept both

Re: Surprising behaviour of std.experimental.allocator

2020-12-26 Thread ag0aep6g via Digitalmars-d-learn
On 26.12.20 13:59, ag0aep6g wrote: Looks like a pretty nasty bug somewhere in std.experimental.allocator or (less likely) the GC. Further reduced code: [...] Apparently, something calls deallocateAll on a Mallocator instance after the memory of that instance has been recycled by

  1   2   3   4   5   6   7   8   >