Re: Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?

2022-04-04 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 4 April 2022 at 07:32:00 UTC, rempas wrote: In other terms, do these functions auto-initialize memory to be ready for use? No. Neither `malloc` nor `realloc` (for which D's `pure...` variants are mere wrappers) are specified to initialize allocated memory. `calloc`, however, is - i

Re: How to print or check if a string is "\0" (null) terminated in the D programming language?

2022-04-06 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 6 April 2022 at 08:55:43 UTC, BoQsc wrote: I have a feeling that some parts of my code contains unterminated strings and they do overflow into other string that is to be combined. I'd like to take a look at strings, analyse them manually and see if any of them end up terminated o

Re: A weird example of .toUTF16z concatination side-effects in wcsncat

2022-04-07 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 7 April 2022 at 10:50:35 UTC, BoQsc wrote: wchar_t* clang_string = cast(wchar_t *)"AA"; You're witnessing undefined behavior. "AA" is a string literal and is stored in the data segment. Mere cast to wchar_t* does not make writing through that poin

Re: How to implement this?

2022-04-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 8 April 2022 at 05:53:03 UTC, Elvis Zhou wrote: assumeNoEscapeOrWhatever!DynamicArray structs; structs ~= cast(A*)&b; is it possible? That's what `@trusted` is for. And that's also why it should be used with care, and on the smallest code possible. ```d struct A {} struct B { A

Re: Nested function requires forward declaration?

2022-04-14 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 14 April 2022 at 08:55:25 UTC, Chris Katko wrote: Using DMD. v2.098-beta-2 Not sure if right terminology. But I just wrote a nested function that uses a variable outside its body. The capture (right term?) is obvious where the invocation is. However, I have to move the declaratio

Re: save and load a 2d array to a file

2022-04-19 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 06:05:27 UTC, Ali Çehreli wrote: One quirk of rawWrite and rawRead is that they want slices of objects. It is a little awkward when there is just one thing to write and read. Uncompiled but something like this: int i = 42; file.rawWrite(*cast((int[1]*)(&i)));

Re: Lambda Tuple with Map Reduce

2022-04-20 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 20 April 2022 at 08:37:09 UTC, Salih Dincer wrote: On Wednesday, 20 April 2022 at 08:04:42 UTC, Salih Dincer wrote: I get an unexpected result inside the second foreach() loop. Anyone know your reason? It's my fault, here is the solution: ```d foreach(fun; funs) { ra

Re: Variables & kind of memory

2022-04-23 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 23 April 2022 at 03:41:17 UTC, Alain De Vos wrote: Feel free to elaborate. Variables declared at module scope, and static variables in function/aggregate scopes, unless also annotated as `shared` or `__gshared`, are thread-local, therefore are placed in thread-local storage. Th

Re: How to use destroy and free.

2022-04-25 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 25 April 2022 at 10:13:43 UTC, Alain De Vos wrote: Ali, thanks for the answer but i rephrase my question. How to destroy,free , for garbage-collection-cycle in the destructor of this code : // But How to force destroy and free , GC-cycle for heap object i ? Short answer: use `des

Re: Assigning to array of structs with custom constructor

2022-04-25 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 25 April 2022 at 14:36:25 UTC, cc wrote: ```d struct Foo { string s; this(string s) { this.s = s; } } Foo foo = "a"; Foo[] foos = ["a"]; // Error: cannot implicitly convert expression `["a"]` of type `string[]` to `Foo[]` Foo[] foos = cast(Foo[]) ["a"]; // Error: e2ir:

Re: std.typecons Typedef initializers?

2022-04-25 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 25 April 2022 at 23:41:47 UTC, Chris Katko wrote: So to use a typedef'd struct... I have to basically add the original type on top of the typedef'd type every time? Surely it's not this clunky? I mean, why even use a typedef then. Why not use just pair, sPair, vPair, etc as separ

Re: CTFE and BetterC compatibility

2022-04-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 14:21:15 UTC, Claude wrote: This is a long-standing pain point with BetterC (see https://issues.dlang.org/show_bug.cgi?id=19268). As for this: If I compile without the BetterC switch, compilation actually works but I'll have some linker issues: ``` $ gcc test.

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

2022-05-02 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 2 May 2022 at 16:29:05 UTC, Loara wrote: Template deduction for aliased function parameter is a very tricky argument and it's not so simple to handle in certain cases. Consider for example this code: ```d template MyAlias(T){ alias MyAlias = int; } T simp(T)(MyAl

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

2022-05-02 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 2 May 2022 at 20:08:48 UTC, Ali Çehreli wrote: On 5/2/22 12:17, Stanislav Blinov wrote: > On Monday, 2 May 2022 at 16:29:05 UTC, Loara wrote: > >> Template deduction for aliased function parameter is a very tricky >> argument and it's not so simple to handle in certain cases. Consider

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

2022-05-02 Thread Stanislav Blinov via Digitalmars-d-learn
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){   return T.init;     }     int main(){

Re: Making alias of a struct field needs "this".

2020-06-02 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 2 June 2020 at 09:28:01 UTC, realhet wrote: I did it that way: private enum fieldMap = [ // simple names for descriptive and structured fields "hauteur" : "general.height", "rayon" : "profile.radius", "plage" : "profile.plage", "offsetv"

Re: Should it compile?

2020-06-06 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 6 June 2020 at 11:58:06 UTC, Basile B. wrote: On Saturday, 6 June 2020 at 08:55:20 UTC, Jack Applegame wrote: Should it compile? I think, it should. maybe it shouldn't but then with another message, for example Error, cannot `void` initialize a `const` declaration. since tha

Re: Should it compile?

2020-06-06 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 6 June 2020 at 12:54:38 UTC, Stanislav Blinov wrote: The moveEmpalce should compile... But not when the *first* argument is const though, like in the example. For *that*, one would have to insert an additional cast.

Re: Should it compile?

2020-06-07 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 7 June 2020 at 23:09:41 UTC, Jack Applegame wrote: auto const_ua = Unique!(const NonCopyable)(move(ca)); // error, why??? } ``` Moving *from* a const would violate const. At least, until such time that the compiler is finally taught about move() (hopefully, sometime this deca

Re: Arrays and non-copyable elements

2020-06-07 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 8 June 2020 at 00:31:10 UTC, Steven Schveighoffer wrote: This is a bug, please file. What is likely happening is that the template is not moving the data to the underlying C call. -Steve That is not a bug, it's a shortcoming of garbage-collected arrays. D arrays are not equipped

Re: Error: llroundl cannot be interpreted at compile time, because it has no available source code

2020-06-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 8 June 2020 at 18:08:57 UTC, mw wrote: 2) even it does so, but why such simple function as lroundl cannot be CTFE-ed? Because, as the error message states, there's no source for it :) std.math calls into C math library.

Re: filter custom version id from __traits code

2020-06-09 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 9 June 2020 at 17:40:10 UTC, Basile B. wrote: Any idea ? As I replied in the issue report: Instead of static if (!is(mixin(member) == module) && !(is(mixin(member use static if (is(typeof(mixin(member

Re: Finding out ref-ness of the return of an auto ref function

2020-06-12 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 12 June 2020 at 17:50:43 UTC, Arafel wrote: All in all, I still think something like `__traits(isRef,return)` would still be worth adding! After all the compiler already has all the information, so it's just about exposing it. I'm trying to think of a library solution, but I find i

Re: Finding out ref-ness of the return of an auto ref function

2020-06-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 13 June 2020 at 09:13:36 UTC, Arafel wrote: If, however, you're wrapping a function template, however, you won't know until you actually instantiate it, which is basically going back to Paul Backus' solution. So the compiler doesn't always have all the information :) Well, the c

Re: Why is it possible to call non-const member functions of rvalues but a compile error to modify members or rvalues directly?

2020-06-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 13 June 2020 at 11:26:58 UTC, Johannes Loher wrote: Why is it a compile error to set `_a` directly but calling `a` just works fine? If we prevent modifying members of rvalues directly, I would also expect calling non-const member functions of rvalues to be prevented. 1) Constr

Re: Weird behavior with UDAs

2020-06-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 13 June 2020 at 13:08:29 UTC, realhet wrote: How can be a string represented with 'null' by default instead on `""`. Unless I state it explicitly with name="" ? o.O Because string is simply `alias string = immutable(char)[]`, and default initializer for arrays is null.

Re: Initializing an associative array of struct

2020-06-14 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 14 June 2020 at 04:36:09 UTC, Denis wrote: Note also that the defaults for id and value are fine... I would welcome a suggestion for how to initialize the keys of parameters. As there will be a couple dozen of the param string keys, a more succinct method would be preferable over

Re: Should a parser type be a struct or class?

2020-06-17 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 17 June 2020 at 11:50:27 UTC, Per Nordlöw wrote: Should a range-compliant aggregate type realizing a parser be encoded as a struct or class? In dmd `Lexer` and `Parser` are both classes. In general how should I reason about whether an aggregate type should be encoded as a struct

Re: "if not" condition check (for data validation)

2020-06-17 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 17 June 2020 at 23:46:54 UTC, Denis wrote: `if` is not a good substitute, because it works in the opposite sense, often requiring lots of `not`s. As a trivial example: assert( configfile.isFile && configfile.extension == ".conf" ) -vs- if ( !configfile.isFile || configfile

Re: "if not" condition check (for data validation)

2020-06-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 18 June 2020 at 12:13:21 UTC, Denis wrote: THE ESSENTIAL QUESTION Is there a way to write an `unless` operator that would allow the condition to be expressed in an affirmative sense? It would be used like `if`, i.e. something like: unless ( ) { ; // Or even:

Re: "if not" condition check (for data validation)

2020-06-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 18 June 2020 at 13:57:39 UTC, Dukc wrote: No reason to use templates here Pff. Me no think straight. -.-

Re: "if not" condition check (for data validation)

2020-06-19 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 18 June 2020 at 17:39:44 UTC, Denis wrote: I should add that this one made me laugh though, giving flashbacks to that horrible "not speak" of the early 90s: if ( configfile.isFile.not ) ... LOL Approve Yoda does.

Re: are std.traits.FieldNameTuple and std.traits.Fields returned value always in sync?

2020-06-20 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 20 June 2020 at 20:17:54 UTC, mw wrote: Are their returned value, i.e the field names and their types are always in the same order, and of the same length? If they are not, how to get sync-ed pairs (name, type)? If they are, why we need two separate calls, which cause confusion.

Re: Why infinite loops are faster than finite loops?

2020-06-20 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 20 June 2020 at 21:11:57 UTC, tastyminerals wrote: I am not sure that this is a question about D or a more general one. I have watched this nice presentation "Speed Is Found In The Minds of People" by Andrei: https://www.youtube.com/watch?v=FJJTYQYB1JQ&feature=youtu.be?t=2596 and o

Re: are std.traits.FieldNameTuple and std.traits.Fields returned value always in sync?

2020-06-22 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 22 June 2020 at 19:55:29 UTC, mw wrote: Yes, in the same order and of the same length. Can we add this information to the doc? to make it clear to the user: https://dlang.org/library/std/traits.html It's pretty clear in that doc already: alias FieldNameTuple(T) = staticMap!(N

Re: called copy constructor in foreach with ref on Range

2020-06-22 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 22 June 2020 at 20:51:37 UTC, Jonathan M Davis wrote: You're unlikely to find much range-based code that does that and there really isn't much point in doing that. Again, copying isn't the problem. It's using the original after making the copy that's the problem. Copy *is* the pro

Re: Temporary File Creation

2020-06-22 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 22 June 2020 at 21:46:57 UTC, Per Nordlöw wrote: Has anybody written a procedure for creating a temporary file in a race-free manner? And why has such a procedure not already been added to std.file when std.file.tempDir has? See: https://dlang.org/library/std/file/temp_dir.html

Re: called copy constructor in foreach with ref on Range

2020-06-22 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 22 June 2020 at 21:33:08 UTC, H. S. Teoh wrote: Don't be shocked when you find out how many Phobos ranges have .init states that are invalid (e.g., non-empty, but .front and .popFront will crash / return invalid values). Which ones? Jonathan is coming from the POV of generic code.

Re: called copy constructor in foreach with ref on Range

2020-06-22 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 23 June 2020 at 02:41:55 UTC, Jonathan M Davis wrote: As things stand, uncopyable ranges aren't really a thing, and common range idiomns rely on ranges being copyable. Which idioms are those? I mean, genuine idioms, not design flaws like e.g. references. We'd need some major red

Re: called copy constructor in foreach with ref on Range

2020-06-22 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 23 June 2020 at 03:52:23 UTC, Jonathan M Davis wrote: On Monday, June 22, 2020 9:25:55 PM MDT Stanislav Blinov via Digitalmars-d- learn wrote: On Tuesday, 23 June 2020 at 02:41:55 UTC, Jonathan M Davis wrote: > As things stand, uncopyable ranges aren't really a thing, >

Re: called copy constructor in foreach with ref on Range

2020-06-23 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 23 June 2020 at 05:24:37 UTC, H. S. Teoh wrote: On Tue, Jun 23, 2020 at 03:25:55AM +, Stanislav Blinov via Digitalmars-d-learn wrote: On Tuesday, 23 June 2020 at 02:41:55 UTC, Jonathan M Davis wrote: > We'd need some major redesigning to make uncopyable ranges >

Re: Garbage collection

2020-06-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 27 June 2020 at 10:08:15 UTC, James Gray wrote: I find that the memory usage grows to about 1.5GB and never decreases. Is there something I am not understanding? How are you measuring that? GC.collect() does not necessarily release the pages to the OS. For that, there's the GC.mi

Re: Garbage collection

2020-06-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 27 June 2020 at 11:11:38 UTC, James Gray wrote: I am measuring the memory usage using top from the command line. GC.minimize() does seem to stop the leak. That is not a memory leak. That's the allocator keeping pages for itself to not have to go to the kernel every time you alloc

Re: Garbage collection

2020-06-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 27 June 2020 at 11:35:12 UTC, Arafel wrote: If you are using linux, have in mind that the memory is often not returned to the OS even after a (libc) free. That's a good observation. Although a GC implementation is not required to actually use malloc, so depending on that falls in

Re: Garbage collection

2020-06-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 27 June 2020 at 14:12:09 UTC, kinke wrote: Note that I explicitly clear the `str` slice before GC.collect(), so that the stack shouldn't contain any refs to the fat string anymore. Hrm... What happens if you call collect() twice?

Re: Garbage collection

2020-06-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 27 June 2020 at 16:03:12 UTC, kinke wrote: On Saturday, 27 June 2020 at 15:27:34 UTC, Stanislav Blinov wrote: Hrm... What happens if you call collect() twice? Nothing changes, even when collecting 5 times at the end of each iteration. In the filed testcase, I've extracted the s

Re: How to implement Canceleable spawn() from parent

2020-06-28 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 28 June 2020 at 13:29:08 UTC, aberba wrote: Getting error: Error: template std.concurrency.spawn cannot deduce function from argument types !()(void delegate(Tid id) @system, Tid), candidates are: /usr/include/dmd/phobos/std/concurrency.d(460,5): spawn(F, T...)(F fn, T args

Re: How to implement Canceleable spawn() from parent

2020-06-28 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 28 June 2020 at 23:02:26 UTC, aberba wrote: I believe this: StopWatch sw; sw.start; works becuse D structs are initialized by default, right? I've never actually done it this way. Little details. Yup. You can also do a auto sw = StopWatch(AutoStart.yes); and not have to call `st

[DIP1000] Something I don't quite understand regarding 'scope'

2020-06-28 Thread Stanislav Blinov via Digitalmars-d-learn
void local(Args...)(Args args) { } void main() @safe { import std.stdio; scope int* p; local(p); // Ok writeln(p); // Error: scope variable p assigned to non-scope parameter _param_0 calling std.stdio.writeln!(int*).writeln } The signatures of `std.stdio.writeln` and `local`

Re: reference variables don't exist, but can simulate them

2020-06-28 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 29 June 2020 at 02:11:15 UTC, NonNull wrote: Deprecation: Cannot use alias this to partially initialize variable j of type refer. Use j._() This is for the line j=3 What is this about? Where does this hidden rule come from? That one comes from [1]. But there are quite a few more

Re: [DIP1000] Something I don't quite understand regarding 'scope'

2020-06-29 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 29 June 2020 at 06:21:43 UTC, ag0aep6g wrote: Since `local` and `writeln` are templates, the attributes for their parameters are inferred from their bodies. `local!(int*)` doesn't do anything with the parameter, so it's inferred as `scope`. `writeln!(int*)` apparently does something

Re: scope guard question

2020-06-29 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 29 June 2020 at 22:31:12 UTC, Arjan wrote: So when no inner scope is present, the scope exit 'runs' after the return? Is that indeed expected behavior according to the specification? Yes. A scope ends at the '}'. Destructors and scope guards execute then, after the return.

Re: Privatize a few members to allow messing with them #11353

2020-06-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 30 June 2020 at 19:42:57 UTC, matheus wrote: in this case this was more a style thing than anything else right? Or is there something I'm not able to see? Before the change, linnum and charnum are public variables, one can do a += on them. After the change, they become properties

Re: Privatize a few members to allow messing with them #11353

2020-06-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 30 June 2020 at 19:58:05 UTC, matheus wrote: +loc.linnum = loc.linnum + incrementLoc; This works because it was declared: void linnum(uint rhs) { _linnum = rhs; } Right? Almost. Given these definitions: @safe @nogc pure @property { const uint linnum() { return _

Re: idiomatic output given -preview=nosharedaccess ,

2020-06-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 30 June 2020 at 20:04:33 UTC, Steven Schveighoffer wrote: The answer is -- update Phobos so it works with -nosharedaccess :) Yeah... and dip1000. And dip1008. And dip... :)

Re: Progress printing with threads?

2020-07-01 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 1 July 2020 at 07:52:28 UTC, AB wrote: Hello. I am unsure how to proceed about printing progress in my program. Is it a good idea to std.concurrency.spawn a new thread?.. This example code shows my situation: MmFile input = new MmFile(/* ... */); ulong fileSize

Re: Print only part of a stack trace

2020-07-01 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 1 July 2020 at 18:30:15 UTC, Dennis wrote: I have a function that checks a global error constant of a C library (OpenGL) like this: ``` void assertNoOpenGLErrors() { if (glGetError() != GL_NO_ERROR) { assert(0); // stack trace points to here instead of caller } }

Re: BetterC Bug? Intended Behavior? Asking Here As Unsure

2020-07-06 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 6 July 2020 at 20:06:51 UTC, Kayomn wrote: Something discovered in the D Language Code Club Discord server with the help of Wild is that the following code: struct Test { ~this() {} } void tester(Test test, Test[] tests...) { } extern(C) void main() { tester(Test(), Test()); }

Re: opApply and attributes

2020-07-07 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 7 July 2020 at 13:33:41 UTC, Paul Backus wrote: You can make opApply a template: int opApply(Dg)(Dg dg) if (is(Dg : scope int delegate(ref E))) { // etc. } Because `scope int delegate(ref E) @safe` implicitly converts to `scope int delegate(ref E)`, thi

Re: constructing labels for static foreach inside switch inside foreach

2020-07-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 July 2020 at 02:06:01 UTC, Steven Schveighoffer wrote: Seems simple enough, except that this inner portion is unrolled, and if I have more than one type to run this on, I already have an "innerloop" label defined. Is there a way to define a label using a mixin or something? o

Re: How to ensure template function can be processed during compile time

2020-07-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 July 2020 at 20:11:05 UTC, IGotD- wrote: int v; enum sz = mySize!int // works, returns 46 enum sz2 = mySize(v) // doesn't work. Error: variable v cannot be read at compile time Here we have a difference between C++ and D as C++ was able infer the size of v during compile tim

Re: What's the point of static arrays ?

2020-07-10 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 10 July 2020 at 10:13:23 UTC, wjoe wrote: So many awesome answers, thank you very much everyone! Less overhead, Using/needing it to interface with something else, and Efficiency are very good points. However stack memory needs to be allocated at program start. I don't see a huge ben

Re: Bitfileds Error: no identifier for declarator

2021-10-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 28 October 2021 at 05:20:35 UTC, data pulverizer wrote: I am trying to compile the following items: struct sxpinfo_struct { mixin(bitfields!( // ... uint, "debug",1, // ... } ``` But I get the error... `debug` is a language keyword, try a different one, like

Re: Linker issues with struct postblit

2021-10-29 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 29 October 2021 at 11:05:14 UTC, Imperatorn wrote: On Thursday, 28 October 2021 at 01:39:10 UTC, Thomas Gregory wrote: I am a maintainer of the [dhtslib](https://github.com/blachlylab/dhtslib) package and I have been running into issues with a new implementation of reference countin

Re: Does associative array change the location of values?

2021-10-29 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 29 October 2021 at 21:00:48 UTC, Steven Schveighoffer wrote: This is incorrect, the buckets are each heap allocated. Just the array of bucket pointers would change. In addition, AAs do not deallocate the key/value pairs ever. You are safe to obtain a pointer to a value and it will

Re: Does associative array change the location of values?

2021-10-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 30 October 2021 at 11:59:15 UTC, Steven Schveighoffer wrote: It should be documented. There isn't a valid way to remove these requirements, even if they are currently just an implementation detail -- code already depends on these properties. And D is a GC-based language, especi

Re: Does associative array change the location of values?

2021-10-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 30 October 2021 at 16:55:03 UTC, Steven Schveighoffer wrote: auto v = k in aa; aa.remove(k); How can the GC/compiler work out that there is still a reference? ??? The same way it does for all other references. I think either you misunderstood me, or I misunderstood you.

Re: Does associative array change the location of values?

2021-10-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 30 October 2021 at 17:45:57 UTC, Steven Schveighoffer wrote: You said "deallocating unreferenced elements". I thought you meant elements unreferenced by the AA. Yup, I misunderstood you :) What I mean is, the AA isn't going to change implementations where it now deallocates val

Re: Does associative array change the location of values?

2021-10-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 30 October 2021 at 18:31:16 UTC, Andrey Zherikov wrote: I did small test and it printed the same values three times so even rehash doesn't change the address of the value: So it seems pretty safe to store a pointer to a value in AA. And I agree that this should definitely be doc

Re: Does associative array change the location of values?

2021-10-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 30 October 2021 at 20:19:58 UTC, Imperatorn wrote: https://dlang.org/spec/garbage.html#pointers_and_gc What test could be written to verify the behaviour? Assuming the GC was moving? You'd need a loop allocating different sizes, storing the addresses somewhere the GC won't see

Re: Does associative array change the location of values?

2021-10-30 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 30 October 2021 at 22:47:57 UTC, Elronnd wrote: If the GC were moving, it would also have to move the pointers you took to AA elements. You would never get stale pointers in any event. Who said you would?..

Re: Completing C code with D style

2021-11-03 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 00:50:51 UTC, Siarhei Siamashka wrote: !text.join("\n").writeln; Ahem... You've turned a program that does not allocate to a program that allocates who knows how much memory? And Ali... associative arrays? For this? What are you trying to teach the goo

Re: How do I assign attributes of a function to another function?

2021-11-05 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 5 November 2021 at 06:19:16 UTC, Li30U wrote: I am creating a templated object that is a storehouse for a heap object and executes their methods and returns an array of results. With the help of a template, I want to achieve this, but I want to assign the same attributes to the funct

Re: How do I assign attributes of a function to another function?

2021-11-05 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 5 November 2021 at 06:19:16 UTC, Li30U wrote: ...e.g. ```d // ... mixin ("ReturnType /*...snip...*/ " ~ member ~ "()(Parameters! /*...snip...*/ ``` Note the `()` before parameter list. This would make your member function a function template, for which attributes will be inferred

Re: auto ref function parameter causes that non copyable struct is copied?

2021-11-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 8 November 2021 at 23:26:39 UTC, tchaloupka wrote: ``` auto gen() { Foo f; // <--- this one f.n = 42; return value(f.move()); } void main() { Foo f; f = gen().unwrap.move; } ``` ~this(0) ~this(0) ~this(0) ~this(42) <- this is a copy (that shouldn

Re: Completing C code with D style

2021-11-09 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 9 November 2021 at 11:03:09 UTC, forkit wrote: They both produce exactly the same output. But do vastly different things. But I tell ya.. the cognitive load .. well.. it increased dramatically ;-) Of course it did. Cuz you overthunk it. Dramatically. Your D version allocates m

Re: Completing C code with D style

2021-11-09 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I really don't give a damn about allocations .. not one little bit ;-) That's not the point. The point is the program is doing unnecessary non-trivial work while intr

Re: Wrong result with enum

2021-11-10 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 05:37:05 UTC, Salih Dincer wrote: is this a issue, do you need to case? ```d enum tLimit = 10_000; // (1) true result enum wLimit = 100_000; // (2) wrong result ``` https://dlang.org/spec/enum.html#named_enums Unless explicitly set, default type is int. 1

Re: Wrong result with enum

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 09:11:37 UTC, Salih Dincer wrote: Unless explicitly set, default type is int. 110 is greater than int.max. 11 ```d enum w = 100_000; size_t b = w * w; // size_t b = 10 * 10; // ??? assert(b == 10_000_000_000); // Assert Failure ``` Th

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 23:15:09 UTC, forkit wrote: On Wednesday, 10 November 2021 at 22:17:48 UTC, russhy wrote: On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I really don't give a damn about allocations

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 00:11:07 UTC, H. S. Teoh wrote: It depends on what you're doing. In the OP's example, yeah worrying about allocations is totally blowing things out of proportions. But that's the thing. How would one ever learn to know where that dividing line is if all the l

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 21:56:19 UTC, Ali Çehreli wrote: On 11/11/21 11:34 AM, Stanislav Blinov wrote: > Pessimization, though, is laughably easy, and > should be avoided at all costs. I am not passionate about this topic at all and I am here mostly because I have fun in this forum. S

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 22:10:04 UTC, forkit wrote: It's called 'staged learning'. Staged learning is the only way for humans to learn, due to the limitations of the human cognitive system. Specifically, the way short-term memory and long-term memory facilitate learning. Those who

Re: using __traits to get line number of a member

2021-11-12 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 13 November 2021 at 05:31:51 UTC, forkit wrote: Code below is self explanatory. Any assistance on how to get the line number is welcome ;-) https://dlang.org/spec/traits.html#getLocation That?

Re: using __traits to get line number of a member

2021-11-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 13 November 2021 at 08:04:56 UTC, forkit wrote: int i; foreach(m; __traits(allMembers, mixin(__MODULE__))) // ... __traits(getLocation, mixin(m))[1]); What you really should be doing is this: ```d static import mod = mixin(__MODULE__); foreach (i, name; __traits(a

Re: using __traits to get line number of a member

2021-11-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Sunday, 14 November 2021 at 04:05:45 UTC, forkit wrote: However, there is no isClass method. Why not? How do I determine if a member is a class.. I wonder... ``` static if (is(something == class)) { /* ... */ } ``` or, if member is an instance ``` static if (is(typeof(something) == class

Re: Any additions for write-to-file short program

2021-11-18 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 18 November 2021 at 22:20:48 UTC, pascal111 wrote: In next program that rewrites original written texts into new files, I see that it may need some additions or we can accept it like this because it's just a simple program that achieve its task and doesn't need any philosophical ad

Re: Include .def definition file information for the linker into a .d source file

2021-11-25 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 25 November 2021 at 09:00:52 UTC, Imperatorn wrote: What most ppl do in that case is to just provide a script, for example build.cmd that just does what it needs. The user just clicks the script and it does everything for them. "How can I make it so that I don't need an extra fil

Re: bool empty() const for ranges

2021-11-26 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 26 November 2021 at 10:44:10 UTC, Salih Dincer wrote: * Is the const essential for ranges? * Is it possible to rewind the pointer (```Node * head;```) when my head is empty by the const? `empty` is not required to be `const`, but it is required to yield the same result if called m

Re: sleeping vs sched_yield

2021-12-02 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 2 December 2021 at 23:29:17 UTC, Chris Katko wrote: there's: ```d import core.thread; Thread.sleep( dur!("msecs")(10) ); ``` but what if you want to simply yield all remaining time back to the time scheduler? Is there a D std.library accessible version of POSIX sched_yield:

Re: Mixin template overloads not working

2021-12-03 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 3 December 2021 at 10:42:37 UTC, Rumbu wrote: Bug or feature? Is there any workaround? The error message explains what to do :) Error: class `mixinover.AnotherVisitor` use of `mixinover.Visitor.visit(S s)` is hidden by `AnotherVisitor`; use `alias visit = Visitor.visit;` to intro

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 08:07:59 UTC, Petar Kirov [ZombineDev] wrote: ```d interface ICallable { void opCall() const; } alias Action = void delegate(); struct A { Action[] dg; } ``` At this point why not just call a spade a spade and store an array of ICallables directly?

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:18:23 UTC, forkit wrote: It's also worth noting the differences in compiler output, as well as the time taken to compile, these two approaches: (1) string str = "abc;def;ab".filter!(c => c != ';').to!string; (2) string str = "abc;def;ab".replace(";", "");

Re: How to loop through characters of a string in D language?

2021-12-10 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 10 December 2021 at 13:22:58 UTC, Matheus wrote: My C way of thinking while using D: import std; string stripsemicolons(string input){ char[] s = input.dup; int j=0; for(int i=0;i Oooh, finally someone suggested to preallocate storage for all these reinventions of the

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 10 December 2021 at 23:53:47 UTC, Ola Fosheim Grøstad wrote: ```d char[] dontdothis(string s, int i=0, int skip=0){ if (s.length == i) return new char[](i - skip); if (s[i] == ';') return dontdothis(s, i+1, skip+1); auto r = dontdothis(s, i+1, skip); r[i-skip] = s[i];

Re: How to loop through characters of a string in D language?

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:34:17 UTC, Ola Fosheim Grøstad wrote: void donttrythisathome(string s, char stripchar) @trusted { import core.stdc.stdlib; char* begin = cast(char*)alloca(s.length); A function with that name, and calling alloca to boot, cannot be @trusted ;)

Re: Why code failed to compile for foo2?

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 11 December 2021 at 22:59:52 UTC, Adam Ruppe wrote: On Saturday, 11 December 2021 at 22:50:45 UTC, apz28 wrote: void foo2(T)(Unqual!T x) if(isUnsigned!T) {} This means it treats foo2 as if it doesn't exist unless T is unsigned... onlineapp.d(15): Error: template `onlineapp.foo2

Re: Why code failed to compile for foo2?

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 11 December 2021 at 23:44:59 UTC, Adam Ruppe wrote: On Saturday, 11 December 2021 at 23:17:17 UTC, Stanislav Blinov wrote: ? No. If it was unsatisfied constraint, the error would've shown that. And if you try to instantiate it, you'll see it is an unsatisfied constraint anyway. T

Re: A debug class has started

2021-12-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:58:42 UTC, forkit wrote: immutable(char)[] replaceChar(char* str, ulong len, char ch1, char ch2) //snip return to!(immutable(char)[])(str); } You're calling a `to` on a char pointer, which, ostensibly, would look for null terminator. Which there may not

Re: Immutability and arrays

2021-12-14 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 08:44:02 UTC, rumbu wrote: I am trying to understand why in this two different cases (Simple and Complex), the compiler behaviour is different. ```d struct SimpleStruct { int x;} struct ComplexStruct { int[] x; } void main() { SimpleStruct[] buf1; immuta

  1   2   3   >