Re: nothrow and std.exception.ifThrown

2021-04-29 Thread Ali Çehreli via Digitalmars-d-learn
On 4/29/21 9:02 AM, novice2 wrote: > format() can throw. In order to throw for an int, I added a foo(x) expression to prove that the code works. > I don't want embrace format into try..catch block, > and i found elegant std.exception.ifThrown. There are collectException and collectExceptionM

Re: Selected elements from splitter output

2021-05-04 Thread Ali Çehreli via Digitalmars-d-learn
On 5/4/21 1:40 PM, Chris Piker wrote: > I only care about columns 0, 2, 3, 4, 8, 9, 10. That's std.range.stride. > char[][] wanted = string_range.get( [1, 5, 7] ); // pseudo-code element That's std.range.indexed. import std.range; import std.stdio; void main() { auto r = 10.iota.stride(2)

Re: Selected elements from splitter output

2021-05-04 Thread Ali Çehreli via Digitalmars-d-learn
On 5/4/21 3:02 PM, Ali Çehreli wrote: >// Note: The above works only because 'stride' applies >// "design by introspection" (DbI) and is able to work as a >// RandomAccessRanges. Ok, I was too enthusiastic there. The RandomAccessRange'ness of the input range changes how efficient st

Re: Pointer indirections

2021-05-04 Thread Ali Çehreli via Digitalmars-d-learn
On 5/4/21 3:06 PM, Imperatorn wrote: >> Is there any limitation as to the number of pointer indirections? No. >> I have a construct: >>a.b.c.d = x; >> where all the variables are pointers to struct (the same struct). >> >> This fails with 'Access Violation', >> but if I change the code to:

Re: Measure cpu time

2021-05-07 Thread Ali Çehreli via Digitalmars-d-learn
On 5/7/21 1:25 AM, Andre Pany wrote: > get the cpu time For the sake of completeness, that kind of functionality is provided by operating systems as well. For example, on Linux, it is common to run the program through 'time' to get how much wall clock advanced, how much time was spent actuall

Re: Measure cpu time

2021-05-07 Thread Ali Çehreli via Digitalmars-d-learn
On 5/7/21 9:46 AM, Alain De Vos wrote: I see, https://dlang.org/phobos/core_stdc_time.html does not contain the function : "clock_gettime" Weird ... stdc is about D bindings for C's std headers. I don't think clock_gettime is a standard C function. grep'ping under /usr/include/dmd reveals tha

Re: Thread local variables in betterC

2021-05-09 Thread Ali Çehreli via Digitalmars-d-learn
On 5/9/21 11:49 AM, Blatnik wrote: Also, why did it work for 32-bits then? This is a long shot, but are you on OSX? I think TLS is handled specially for 32-bit OSX and that may somehow work in this case. (?) Ali

Re: Near-simplest route to learn D

2021-05-12 Thread Ali Çehreli via Digitalmars-d-learn
On 5/12/21 12:37 PM, Berni44 wrote: > Even if it is a few years old, I would still use the book from Ali. Most > is still valid and maybe, the online version is even updated Yes, the online version is more up-to-date than the print version. (By the way, the more-up-to-date online PDF is what wo

Re: Passing a byLine as an argument to InputRange

2021-05-13 Thread Ali Çehreli via Digitalmars-d-learn
On 5/13/21 10:07 AM, Jeff wrote: > I have a class where I'd like to supply it with an InputRange!string. > Yet, for the life of me I can't seem to pass to it a File.byLine As Adam said, your range elements need to be converted to string e.g. with 'text' (the same as to!string). However, you mus

Re: Passing a byLine as an argument to InputRange

2021-05-13 Thread Ali Çehreli via Digitalmars-d-learn
On 5/13/21 11:29 AM, Ali Çehreli wrote: create a dynamic InputRange!string object for dynamic polymorphism that InputRange provides. And that's achieved by function inputRangeObject(): Some examples: http://ddili.org/ders/d.en/ranges_more.html#ix_ranges_more.polymorphism,%20run-time Ali

Re: ref struct member function

2021-05-13 Thread Ali Çehreli via Digitalmars-d-learn
On 5/13/21 12:40 PM, Steven Schveighoffer wrote: On 5/13/21 3:21 PM, PinDPlugga wrote: This works but issues a deprecation warning: ``` onlineapp.d(30): Deprecation: returning `this` escapes a reference to parameter `this` onlineapp.d(30):    perhaps annotate the parameter with `return` F

Re: Recommendations on avoiding range pipeline type hell

2021-05-15 Thread Ali Çehreli via Digitalmars-d-learn
On 5/15/21 4:25 AM, Chris Piker wrote: > But, loops are bad. I agree with Adam here. Although most of my recent code gravitates towards long range expressions, I use 'foreach' (even 'for') when I think it makes code more readable. > Is there some obvious trick or way of looking at the proble

Re: Anyone tried?

2021-05-17 Thread Ali Çehreli via Digitalmars-d-learn
On 5/17/21 10:49 AM, Imperatorn wrote: https://www.educative.io/courses/programming-in-d-ultimate-guide That's the interactive version of (first half of) "Programming in D"[1]. The entire conversion was done by Educative and some editing was added by them. The main difference is the ability t

Re: Is unix time function in wrong module?

2021-05-19 Thread Ali Çehreli via Digitalmars-d-learn
On 5/19/21 12:18 PM, Martin wrote: > shouldn't the unix time functions be in std.datetime.Date and > std.datetime.DateTime instead of std.datetime.SysTime? I wouldn't expect those modules to publicly import unix time function. Such functions are found under the core.sys package: import std.st

Re: Working with ranges

2021-05-26 Thread Ali Çehreli via Digitalmars-d-learn
On 5/26/21 8:07 AM, Jack wrote: maybe array from std.array to make that range in array of its own? Yes, something like this: import std; void main() { auto arr = 10.iota.map!(i => uniform(0, 100)); auto starts = arr[0..$].stride(2); auto ends = arr[1..$].stride(2); auto randomNumbe

Re: How to work around the infamous dual-context when using delegates together with std.parallelism

2021-05-27 Thread Ali Çehreli via Digitalmars-d-learn
On 5/27/21 2:58 AM, Christian Köstlin wrote: > writeln(taskPool.amap!(user => servers.doSomething(user))(users)); Luckily, parallel() is a free-standing function that does not require a "this context". Is the following a workaround for you? auto result = new string[users.length]; use

Re: How to work around the infamous dual-context when using delegates together with std.parallelism

2021-05-27 Thread Ali Çehreli via Digitalmars-d-learn
On 5/27/21 9:19 AM, Ali Çehreli wrote:   auto result = new string[users.length];   users.enumerate.parallel.each!(en => result[en.index] = servers.doSomething(en.value));   writeln(result); I still like the foreach version more: auto result = new string[users.length]; foreach (i,

Re: foreach: How start a foreach count with specific number?

2021-06-02 Thread Ali Çehreli via Digitalmars-d-learn
On 6/2/21 8:49 AM, Marcone wrote: > But I don't want it starts with 0, but other number. How can I do it? It is not configurable but is trivial by adding a base value: import std.stdio; enum base = 17; void main() { auto arr = [ "hello", "world" ]; foreach (i, str; arr) { const count

Re: How use lineSplitter with KeepTerminator flag?

2021-06-09 Thread Ali Çehreli via Digitalmars-d-learn
On 6/9/21 2:48 PM, Marcone wrote: I want add Yes.keepTerminator flag on lineSplitter. It's a template parameter, which is specified after an exclamation mark. The trouble is, Flag is defined in std.typecons. I would expect std.typecons (or at least Flag from it) to be publicly imported but it

Re: Wrote a Protobuf-like D (de)serialisation library - asking for comments / suggestions

2021-06-12 Thread Ali Çehreli via Digitalmars-d-learn
On 6/12/21 2:59 PM, Sinisa Susnjar wrote: > instead uses the meta programming facilities of D to (de)serialise D data types from/to binary messages. Yeah! :) I did the same at work. > (I am sure there is a lot of room for improvement) Without reading your code carefully, I can think of two

Re: Unpacking Slices

2021-06-14 Thread Ali Çehreli via Digitalmars-d-learn
On 6/14/21 11:08 AM, Justin Choi wrote: Is there any shortcut for unpacking slices like I'd want to do in a scenario like this? `info = readln.strip.split;` `string a = info[0], b = info[1], c = info[2];` D does not have automatic unpacking. Here is a quick, dirty, and fun experiment: struc

Re: Parallel For

2021-06-15 Thread Ali Çehreli via Digitalmars-d-learn
On 6/14/21 11:39 PM, seany wrote: > I know that D has parallel foreach [like > this](http://ddili.org/ders/d.en/parallelism.html). I gave an example of it in my DConf Online 2020 presentation as well: https://www.youtube.com/watch?v=dRORNQIB2wA&t=1324s > int[] c ; >

Re: How to translate this C macro to D mixin/template mixin?

2021-06-15 Thread Ali Çehreli via Digitalmars-d-learn
On 6/15/21 5:18 AM, VitaliiY wrote: > STOREBITS and ADDBITS use variables defined in STARTDATA If possible in your use case, I would put those variables in a struct type and make add() a member function. However, a similar type already exists as std.bitmanip.BitArray. Ali

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

2021-06-15 Thread Ali Çehreli via Digitalmars-d-learn
On 6/14/21 10:17 PM, mw wrote: > I think there is another convention (although it's not formally > enforced, but should be) is: > > -- `obj.front() [should be] const`, i.e. it shouldn't modify `obj`, so > can be called multiple times at any given state, and produce the same > result In other wor

Re: Can not get struct member addresses at compile time

2021-06-16 Thread Ali Çehreli via Digitalmars-d-learn
On 6/16/21 2:27 AM, Doeme wrote: > How does one get the address of a struct member? Here is an experiment with offsetof and opDispatch: struct Foo{ ubyte bar; int i; } auto addrOf(T)(ref T t) { static struct AddrOf { void * origin; auto opDispatch(string member)() { return or

Re: Can not get struct member addresses at compile time

2021-06-16 Thread Ali Çehreli via Digitalmars-d-learn
On 6/16/21 8:47 AM, Doeme wrote: > On Wednesday, 16 June 2021 at 13:36:07 UTC, Ali Çehreli wrote: >> On 6/16/21 2:27 AM, Doeme wrote: >> >> > How does one get the address of a struct member? >> >> Here is an experiment with offsetof and opDispatch: > > Cool stuff! > I actually tried a very simila

Re: Can not get struct member addresses at compile time

2021-06-16 Thread Ali Çehreli via Digitalmars-d-learn
On 6/16/21 3:27 PM, Doeme wrote: On Wednesday, 16 June 2021 at 22:16:54 UTC, H. S. Teoh wrote: The compiler does not (and cannot) know.  But the runtime dynamic linker can, and does.  The two are bridged by the compiler emitting a relocatable symbol for the address of the global variable, with

Re: List of Dynamic Arrays

2021-06-17 Thread Ali Çehreli via Digitalmars-d-learn
On 6/17/21 9:10 AM, Justin Choi wrote: >> DList!(int[])() ? > > Thanks I've mentally slapped myself a hundred times for this mistake :D Also, unless DList is really needed, why not: int[][] Ali

Re: Arrays of variants, C++ vs D

2021-06-17 Thread Ali Çehreli via Digitalmars-d-learn
On 6/17/21 1:46 PM, Steven Schveighoffer wrote: > Implicit construction is supported: > > struct Foo > { > int x; > this(int y) { x = y; } > } > > Foo f = 5; // ok implicit construction That's so unlike the rest of the language that I consider it to be a bug. :) Really, why? What if the

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/20/21 8:59 PM, someone wrote: I often need to iterate through a filtered collection (associative array) as following: ```d string strComputerIDunwanted = "WS2"; /// associative array key to exclude foreach (strComputerID, udtComputer; udtComputers) { /// ..remove!(a => a == strComputerID

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

2021-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/20/21 10:36 PM, mw wrote: i.e append an array of elements into another array: ```Python x = [1, 2, 3] x.extend([4, 5]) print(x)  # [1, 2, 3, 4, 5] ``` Thanks. There is also std.range.chain, which can visit multiple ranges in sequence without copying elements. This is a lifesaver when t

Re: Default values in passing delegates to functions

2021-06-23 Thread Ali Çehreli via Digitalmars-d-learn
On 6/23/21 9:16 AM, Anonamoose wrote: > I have a script in which I want a special case where someone can input > something like a potential or a dispersion relation for use in physics > simulations. I want to clean up the implementation for users as not > every situation requires these. So I wrot

Re: is it possible to sort a float range ?

2021-06-23 Thread Ali Çehreli via Digitalmars-d-learn
On 6/23/21 6:09 PM, mw wrote: > I think in most other popular language's std lib: > > container.sort(); > > or > > sort(container); > > > is just one call, and the user will expect the `container` is sorted > after the call. That's exactly the same in D. What's different is, D's sort() does not

Re: How to call stop from parallel foreach

2021-06-24 Thread Ali Çehreli via Digitalmars-d-learn
On 6/24/21 1:33 PM, Bastiaan Veelo wrote: > distributes the load across all cores (but one). Last time I checked, the current thread would run tasks as well. Ali

Re: How to call stop from parallel foreach

2021-06-24 Thread Ali Çehreli via Digitalmars-d-learn
On 6/24/21 1:41 PM, seany wrote: > Is there any way to control the number of CPU cores used in > parallelization ? Yes. You have to create a task pool explicitly: import std.parallelism; void main() { enum threadCount = 2; auto myTaskPool = new TaskPool(threadCount); scope (exit) { m

Re: How to call stop from parallel foreach

2021-06-25 Thread Ali Çehreli via Digitalmars-d-learn
On 6/25/21 6:53 AM, seany wrote: > I tried this . > > int[][] pnts ; > pnts.length = fld.length; > > enum threadCount = 2; > auto prTaskPool = new TaskPool(threadCount); > > scope (exit) { > prTaskPool.finish(); > } > >

Re: How to call stop from parallel foreach

2021-06-25 Thread Ali Çehreli via Digitalmars-d-learn
On 6/25/21 7:21 AM, seany wrote: > The code without the parallel foreach works fine. No segfault. That's very common. What I meant is, is the code written in a way to work safely in a parallel foreach loop? (i.e. Is the code "independent"?) (But I assume it is because it's been the common the

Re: Remove array element within function

2021-07-05 Thread Ali Çehreli via Digitalmars-d-learn
On 7/5/21 8:14 AM, Rekel wrote: > On Monday, 5 July 2021 at 14:22:24 UTC, jfondren wrote: >> What use of long? >> >> remove returns the same type of range as it gets: > > My apology, I meant to say countUntil instead of remove in that context. countUntil says it returns ptrdiff_t, which is the s

Re: Template arg deduction

2021-07-07 Thread Ali Çehreli via Digitalmars-d-learn
On 7/7/21 12:14 PM, Kevin Bailey wrote: > fairly simple template argument deduction It's not the simplest but it is still complicated. :) > I guess D can't crack open a type like that? There are other ways of achieving the same thing the simplest of which is probably the following: void fun

Re: Why I'm getting this "Range Violation" error?

2021-07-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/9/21 12:21 AM, rempas wrote: >while (prompt[i] != '{' && i < len) { In addition to what others said, you can take advantage of ranges to separate concerns of filtering and iteration. Here are two ways: import core.stdc.stdio; import std.algorithm; // Same as your original void print

Re: Where is "open" in "core.sys.linux.unistd"?

2021-07-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/9/21 8:31 AM, rempas wrote: > I searched "fnctl" in the repo [...] Probably made a typo Yes, the typo should be obvious to the non-dyslexic among us. :) fnctl <-- wrong fcntl <-- correct Ali

Re: Can I get the time "Duration" in "nsecs" acurracy?

2021-07-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/9/21 1:54 PM, Paul Backus wrote: On Friday, 9 July 2021 at 20:43:48 UTC, rempas wrote: I'm reading the library reference for [core.time](https://dlang.org/phobos/core_time.html#Duration) and It says that the duration is taken in "hnsecs" and I cannot understand if we can change that and c

Re: assert(false) and GC

2021-07-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/8/21 11:11 AM, DLearner wrote: Hi Please confirm that: `    assert(false, __FUNCTION__ ~ "This is an error message"); ` Will _not_ trigger GC issues, as the text is entirely known at compile time. Best regards One way of forcing compile-time evaluation in D is to define an enum (whi

Re: assert(false) and GC

2021-07-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/9/21 4:12 PM, russhy wrote: >> One way of forcing compile-time evaluation in D is to define an enum >> (which means "manifest constant" in that use). That's all I meant. It was a general comment. > this is very bad, assert are good because they are one liner, making it > 2 line to avoid GC

Re: Can I get the time "Duration" in "nsecs" acurracy?

2021-07-10 Thread Ali Çehreli via Digitalmars-d-learn
On 7/9/21 11:28 PM, rempas wrote: > So it's an OS thing? Don't listen to me on this. :) A quick search yesterday made me believe you need kernel support as well. Even if so, I would imagine modern kernel on modern hardware should work. Ali

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

2021-07-10 Thread Ali Çehreli via Digitalmars-d-learn
On 7/10/21 10:20 PM, someone wrote: > mixin template templateUGC ( > typeStringUTF, > alias lstrStructureID > ) { > > public struct lstrStructureID { The only way that I know is to take a string parameter and use it with a string mixin: mixin template templateUGC ( typeStri

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

2021-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 7/12/21 3:35 PM, someone wrote: >>> private size_t pintSequenceCurrent = cast(size_t) 0; > >> Style: There's no need for the casts (throughout). > > [...] besides, it won't hurt, and it helps me in many ways. I think you are doing it only for literal values but in general, casts can be very

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

2021-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 7/12/21 5:42 PM, someone wrote: > On Monday, 12 July 2021 at 23:25:13 UTC, Ali Çehreli wrote: >> On 7/12/21 3:35 PM, someone wrote: >> >> >>> private size_t pintSequenceCurrent = cast(size_t) 0; >> > >> >> Style: There's no need for the casts (throughout). >> > >> > [...] besides, it won't hur

Re: Error with implicit cast of ^^=

2021-07-13 Thread Ali Çehreli via Digitalmars-d-learn
On 7/13/21 4:12 AM, wjoe wrote: > ```D > byte x = some_val; > long y = some_val; > > x ^^= y; // Error: cannot implicitly convert expression > pow(cast(long)cast(int)x, y) of type long to byte [...] > I rewrote it to something like > ```D > mixin("x = cast(typeof(x))(x" ~ op[0..$-1] ~ " y);");

Re: Error with implicit cast of ^^=

2021-07-14 Thread Ali Çehreli via Digitalmars-d-learn
On 7/14/21 2:44 AM, wjoe wrote: >> x = (x ^^ y).to!(typeof(x)); >> } >> >> For example, run-time error if y == 7. > I was planning on adding support for over-/underflow bits but this is > much better. Thanks! If so, then there is also std.experimental.checkedint: https://dlang.org/phobos/s

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Ali Çehreli via Digitalmars-d-learn
On 7/14/21 9:13 AM, Tejas wrote: > ref/*notice this ref*/ int opIndex(int index)return/*NOTICE THE > RETURN*/ { Indeed... I cover that 'ref' here: http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.return%20type,%20operator Two quotes from that section: 1) "it

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Ali Çehreli via Digitalmars-d-learn
On 7/14/21 11:27 AM, Tejas wrote: > the compiler error should have been a dead giveaway > that opIndex was returning a _constant_ value I know you mean "rvalue" but if I may be unnecessarily pedantic, an rvalue can indeed be mutated: struct A { int i; void mutate() { i = 42; impor

Re: How to create friends of a class at compile time?

2021-07-15 Thread Ali Çehreli via Digitalmars-d-learn
On 7/15/21 11:24 AM, Tejas wrote: > it seems like seperating chunks of C++ code into seperate modules > and using the > ```d > package(qualifiedIdentifier) > ``` > access specifier seems to be the way to go. That would be necessary if one agreed with C++'s 'private' to begin with. In other word

Re: How to create friends of a class at compile time?

2021-07-15 Thread Ali Çehreli via Digitalmars-d-learn
On 7/15/21 5:46 PM, Ali Çehreli wrote: > On 7/15/21 11:24 AM, Tejas wrote: > > package(qualifiedIdentifier) > Just don't do it Ok, I said all that before realizing what you needed with transpiling C++ to D and the solution of package(qualifiedIdentifier). (And package(qualifiedIdentifier) i

Re: Creating immutable arrays in @safe code

2021-07-16 Thread Ali Çehreli via Digitalmars-d-learn
On 7/16/21 1:19 PM, Dennis wrote: > I like passing around immutable data I think the D community needs to talk more about guidelines around 'immutable'. We don't... And I lack a complete understanding myself. :) To me, 'immutable' is a demand of the user from the provider that the data will

Re: Creating immutable arrays in @safe code

2021-07-16 Thread Ali Çehreli via Digitalmars-d-learn
On 7/16/21 3:21 PM, Dennis wrote: > But `pure` is no silver bullet: Agreed. I occasionally struggle with these issues as well. Here is a related one: import std; void foo(const int[] a) { auto b = a.array; b.front = 42; // Error: cannot modify `const` expression `front(b)` } I think t

Re: function parameters: is it possible to pass byref ... while being optional at the same time ?

2021-07-17 Thread Ali Çehreli via Digitalmars-d-learn
On 7/17/21 6:49 PM, Brian Tiffin wrote: > *Practice makes perfect? No, practice makes permanent.* One reference for that quote is "Classical Guitar Pedagogy" by Anthony Glise, Chapter Nine--Practicing. ;) Ali

Re: function parameters: is it possible to pass byref ... while being optional at the same time ?

2021-07-17 Thread Ali Çehreli via Digitalmars-d-learn
On 7/17/21 8:52 PM, someone wrote: > I am a self-taught programmer Same here. > well before college Not here because I had access to a Sinclair Spectrum only in 1985. :/ > I have a degree in electronics engineering Same here. > one of the things that I felt in love with when I was first exp

Re: Including a file

2021-07-18 Thread Ali Çehreli via Digitalmars-d-learn
On 7/18/21 10:28 AM, Vindex wrote: > ``` > ldc2 source/main.d -ofmyprogram -L-l:libmylib.so.0 > Error: file "thing.json" cannot be found or not in a path specified with -J > ``` > > Is the actual inclusion of the file on import deferred until the link step? I wonder whether it's an ldc thing

Re: Yet another parallel foreach + continue question

2021-07-19 Thread Ali Çehreli via Digitalmars-d-learn
On 7/19/21 5:07 PM, seany wrote: > Consider : > > for (int i = 0; i < max_value_of_i; i++) { > foreach ( j, dummyVar; myTaskPool.parallel(array_to_get_j_from, > my_workunitSize) { > > if ( boolean_function(i,j) ) continue; > double d = expensiveFunction(i,j

Re: Not allowed to globally overload operators?

2021-07-20 Thread Ali Çehreli via Digitalmars-d-learn
On 7/19/21 11:20 PM, Tejas wrote: > trying to create the spaceship operator of C++ Just to make sure, D's opCmp returns an int. That new C++ operator was added to provide the same semantics. Ali

Re: Not allowed to globally overload operators?

2021-07-20 Thread Ali Çehreli via Digitalmars-d-learn
On 7/20/21 11:49 AM, H. S. Teoh wrote: > On Tue, Jul 20, 2021 at 11:32:26AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: >> On 7/19/21 11:20 PM, Tejas wrote: >> >>> trying to create the spaceship operator of C++ >> >> Just to make sure, D's opCmp

Re: enum true, or 1

2021-07-21 Thread Ali Çehreli via Digitalmars-d-learn
On 7/21/21 8:44 PM, Brian Tiffin wrote: > What is the preferred syntax for simple on/off states? I use std.typecons.Flag. It feels very repetitive but that's what we have: import std.typecons; void foo(Flag!"doFilter" doFilter) { if (doFilter) { // ... } } bool someCondition; void ma

Re: associative array with Parallel

2021-07-21 Thread Ali Çehreli via Digitalmars-d-learn
On 7/21/21 11:01 PM, frame wrote: > This is another parallel foreach body conversion question. > Isn't the compiler clever enough to put a synchronized block here? parallel is a *function* (not a D feature). So, the compiler might have to analyze the entire code to suspect race conditions. No,

Re: Initializing a complex dynamic array (with real part from one array, and imaginary from other array)?

2021-07-22 Thread Ali Çehreli via Digitalmars-d-learn
On 7/21/21 2:17 AM, drug wrote: > auto z = zip(x, y) // concatenate two ranges > .map!(a=>Complex!double(a[0],a[1])) // take the current first > element of the first range as the real part and the current first > element of the second range as the imaginary part

Re: enum true, or 1

2021-07-23 Thread Ali Çehreli via Digitalmars-d-learn
On 7/22/21 1:44 PM, Brian Tiffin wrote: > So would there be any cringes Not from me. :) > seeing a skeleton D source file that > always ended with > > ~~~d > /++ > Disclaimer > > This software is distributed in the hope that it will be useful, but > WITHOUT ANY WARRANTY; direct or

Re: How to Fix Weird Build Failure with "-release" but OK with "-debug"?

2021-07-23 Thread Ali Çehreli via Digitalmars-d-learn
On 7/23/21 12:30 PM, apz28 wrote: > The -debug build with passing unit-tests so no problem there. > The -release build is having problem. After make change to accommodate > it, it takes forever to build. I started it yesterday 11AM and it is > still compiling now (more than a day already.) It tak

Re: POST request with std.net.curl

2021-07-23 Thread Ali Çehreli via Digitalmars-d-learn
On 7/23/21 11:11 AM, bachmeier wrote: I'm writing a D program that interacts with the Todoist API using std.net.curl. It's not a problem to do get requests to query tasks, but I cannot find a way to get post to work to create a new task. This is a working bash script, where APIKEY is defined e

Re: Why does Unconst exist?

2021-07-27 Thread Ali Çehreli via Digitalmars-d-learn
On 7/27/21 10:38 PM, Tejas wrote: When I initially saw it, I was hopeful that it would allow me to bypass some of the restrictions of ```const``` , but it literally just takes a type and strips the ```const``` from it, you can't pass a variable to it in order to get rid of ```const``` . What us

Re: Performance issue with fiber

2021-07-28 Thread Ali Çehreli via Digitalmars-d-learn
On 7/28/21 1:15 AM, hanabi1224 wrote: > On Wednesday, 28 July 2021 at 01:12:16 UTC, Denis Feklushkin wrote: >> Spawning fiber is expensive > > Sorry but I cannot agree with the logic behind this statement, the whole > point of using fiber is that, spwaning system thread is expensive, thus > ppl c

Re: Exit before second main with -funittest

2021-07-29 Thread Ali Çehreli via Digitalmars-d-learn
Almost all of my programs are in the following pattern: import std.stdio; void main(string[] args) { version (unittest) { // Don't execute the main program when unit testing return; } try { // Dispatch to the actual "main" function doIt(args); } catch (Exception exc) {

Re: Exit before second main with -funittest

2021-07-29 Thread Ali Çehreli via Digitalmars-d-learn
On 7/29/21 9:22 PM, Mathias LANG wrote: > On Friday, 30 July 2021 at 03:45:21 UTC, Ali Çehreli wrote: >> Almost all of my programs are in the following pattern: >> >> ```D >> import std.stdio; >> >> void main(string[] args) { >> version (unittest) { >> // Don't execute the main program when

Re: Exit before second main with -funittest

2021-07-30 Thread Ali Çehreli via Digitalmars-d-learn
On 7/30/21 3:08 PM, Brian Tiffin wrote: > Don't take to C++, never have, even while watching > cfront and Walter having the first** brain to craft a native compiler. [...] > ** I'm not sure Walter was first first, or close first, or... He wrote the first C++ compiler. (cfront was a C++-to-C tr

Re: Exit before second main with -funittest

2021-07-31 Thread Ali Çehreli via Digitalmars-d-learn
On 7/30/21 7:48 PM, Brian TIffin wrote: > Found, find, C++ too hot for most to handle, so why? I rubbed shoulders with C++ people who would argue to the effect of "C++ was not meant to be for everyone." > And a deeper thanks, Ali, for the book. You're welcome. I am very happy when it is help

Re: Is returning void functions inside void functions a feature or an artifact?

2021-08-02 Thread Ali Çehreli via Digitalmars-d-learn
On 8/2/21 9:42 AM, Rekel wrote: > when would one use an alias instead of a > function/delegate? I haven't used aliases before. alias will match both functions and delegates... and... any symbol at all. So, if you don't have a reason to constain the user, callable template parameters are most u

Re: Find struct not passed by reference

2021-08-03 Thread Ali Çehreli via Digitalmars-d-learn
On 8/2/21 4:06 PM, frame wrote: Is there a way to find a struct which should be passed by reference but accidentally isn't? Maybe with copy constructors? If I understand it correctly, your current value-type needs to be a reference type. Would the following be workable solutions? a) Classes

Re: Name Mangling & its representation of D types

2021-08-03 Thread Ali Çehreli via Digitalmars-d-learn
On 8/3/21 9:43 AM, NonNull wrote: I'd like to understand how any D type is represented as a string by the name mangling done by the compilers. Related article that mentions .mangleof, a property of all symbols: https://dlang.org/blog/2017/12/20/ds-newfangled-name-mangling/ Ali

Re: align dynamic array (for avx friendliness) hints? / possible??

2021-08-03 Thread Ali Çehreli via Digitalmars-d-learn
On 8/3/21 10:50 AM, james.p.leblanc wrote: > **Is there some highly visible place this is already documented? For what it's worth, it appears as "slice from pointer" in my index: http://ddili.org/ders/d.en/pointers.html#ix_pointers.slice%20from%20pointer Admittedly, one needs to know the conc

Re: Find struct not passed by reference

2021-08-03 Thread Ali Çehreli via Digitalmars-d-learn
On 8/3/21 12:22 PM, frame wrote: On Tuesday, 3 August 2021 at 19:19:27 UTC, jfondren wrote: On Tuesday, 3 August 2021 at 19:11:16 UTC, frame wrote: On Tuesday, 3 August 2021 at 16:35:04 UTC, Ali Çehreli wrote: Why foreach() does not accept a pointer? pointers don't come with a length? Where

Re: module search paths

2021-08-03 Thread Ali Çehreli via Digitalmars-d-learn
On 8/3/21 9:51 PM, Brian Tiffin wrote: > I added an `import std.json;`. That did not include the > JSONType enum. It works for me: import std.json; int main() { return JSONType.null_; } > Is there a go to quick and easy way of tracking down > module members? Searching for it at dlang.org

Re: __FILE__

2021-08-04 Thread Ali Çehreli via Digitalmars-d-learn
On 8/4/21 7:17 PM, Steven Schveighoffer wrote: >> The compiler has to evaluate the default argument as constant >> expression in order to use it as default value.. > > This is not true, you can use runtime calls as default values. What??? I just checked and it works! :) string bar() { imp

Re: __traits() to get parameter details only ? ... hasMember looks up everything within

2021-08-05 Thread Ali Çehreli via Digitalmars-d-learn
On 8/4/21 7:21 PM, someone wrote: > somewhere I read > Ali saying there's nothing wrong implementing properties the old-way via > functions because @property has nothing special about it but I can't > state where I read what I am stating so take it with a grain of salt. As I understand it, @prop

Re: best/proper way to declare constants ?

2021-08-05 Thread Ali Çehreli via Digitalmars-d-learn
On 8/5/21 5:11 PM, someone wrote: > Although I have very little experience with D, I second this: > refactoring, even huge refactors, proved to be far more straightforward > than I expected. May I humbly suggest names like Location instead of structureLocation to make refactoring even more stra

Re: Proper way to protect (lock) a struct field after initialization ??

2021-08-08 Thread Ali Çehreli via Digitalmars-d-learn
On 8/8/21 3:11 AM, james.p.leblanc wrote: Hello All. Is there a standard way to protect a field of a struct after the struct has been initialized? Is this possible with a struct? If not, I suppose a class (object) would be needed?  If so, are there any simple pointers to an example of this? T

Re: writef, compile-checked format, pointer

2021-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 8/9/21 3:01 PM, Patrick Schluter wrote: > On Monday, 9 August 2021 at 19:38:28 UTC, novice2 wrote: >> format!"fmt"() and writef!"fmt"() templates >> with compile-time checked format string >> not accept %X for pointers, >> >> but format() and writef() accept it >> >> https://run.dlang.io/is/aQ

Re: writef, compile-checked format, pointer

2021-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 8/9/21 3:46 PM, Adam D Ruppe wrote: > The compile time checker [...] implementation is really really > slow and pretty bloated in codegen too. > > My personal policy is to never use it. My personal policy is to kindly ask you to fix it! :p Ali

Re: writef, compile-checked format, pointer

2021-08-10 Thread Ali Çehreli via Digitalmars-d-learn
On 8/9/21 10:03 PM, nov wrote: > i dreamed that programming will be less-error with compile checking format. I still think so because many errors that can be caught at compile time are moved to run time in non-statically-typed languages. > but reality disappoint :( > message for every error

Re: D has the same memory model as C++

2021-08-11 Thread Ali Çehreli via Digitalmars-d-learn
This is an interesting thread but "memory model" does not cover or mean all of the points discussed here. I can't define it precisely, so I'm leaving it to interested parties to search for themselves. :) Ali

Re: D has the same memory model as C++

2021-08-11 Thread Ali Çehreli via Digitalmars-d-learn
On 8/11/21 12:19 PM, Tejas wrote: Atleast leave some pointers on where to start :( I DuckDuckGo'ed it for you. :) https://en.cppreference.com/w/cpp/language/memory_model Then looked it up at Wikipedia too: https://en.wikipedia.org/wiki/Memory_model_(programming) Ali

Re: I do not understand copy constructors

2021-08-12 Thread Ali Çehreli via Digitalmars-d-learn
On 8/12/21 4:32 AM, Paul Backus wrote: > Qualifying the ctor as `inout` works fine I can see how a DConf Online presention is shaping up in your head. ;) http://dconf.org/2021/online/index.html We need a collective understanding of effective use of such fundamental concepts. Ali

Re: Anyway to achieve the following

2021-08-13 Thread Ali Çehreli via Digitalmars-d-learn
On 8/13/21 1:25 AM, JG wrote: > Suppose one has a pointer p of type T*. > Can on declare variable a of type T which is stored in the location > pointed to by p? You may be looking for core.lifetime.emplace. (core.lifetime is not on dlang.org at the moment for me but it is under /usr/include/dm

Re: How to extend the string class to return this inside the square bracket?

2021-08-13 Thread Ali Çehreli via Digitalmars-d-learn
On 8/13/21 4:08 PM, jfondren wrote: On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote: Isn't there some unario operator template that I can use with lambda to handle a string literal? So, something other than an exact "lit"[0..this.xx(..)] syntax is fine? What didn't you like about `

Re: How to extend the string class to return this inside the square bracket?

2021-08-13 Thread Ali Çehreli via Digitalmars-d-learn
On 8/13/21 4:23 PM, Marcone wrote: > string x = "Hello World!"; > writeln(x[x.indexOf("e")..x.indexOf("r")]); I don't see the usefulness and there are the following problems with it: - Not an algorithmic complexity issue but it sounds to me like a pessimization to go through the elements in li

Re: Drawbacks of exceptions being globally allocated

2021-08-14 Thread Ali Çehreli via Digitalmars-d-learn
On 8/14/21 4:41 AM, Tejas wrote: > What is the drawback of the following "simple" ```@nogc``` exception > creation technique? > > ```d > import std; > void main()@nogc > { > try{ > __gshared a = new Exception("help"); > scope b = a; > throw b; > } > catch

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Ali Çehreli via Digitalmars-d-learn
Lot's of great information and pointers already. I will try from another angle. :) On 8/14/21 11:10 PM, rempas wrote: > So when I'm doing something like the following: `string name = "John";` > Then what's the actual type of the literal `"John"`? As you say and as the code shows, there are two

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Ali Çehreli via Digitalmars-d-learn
On 8/15/21 2:10 AM, Alexandru Ermicioi wrote: >> This may be useful in some cases but in general, these colatteral >> exceptions don't carry much information and I don't think anybody >> looks at them. Usually, the first one is the one that explains the >> error case. > That is just an assumptio

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/17/21 11:11 AM, james.p.leblanc wrote: > auto foo(T)(T a, T b) { ... } > > Now, suppose I need to restrict "T" only certain subsets of variable types. There are template constraints: import std.traits; auto foo(T)(T a, T b) if (isArray!T) { // ... } auto foo(T)(T a, T b) if (isFloati

Re: Non-consistent implicit function template specializations

2021-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/17/21 2:59 AM, Rekel wrote: > template TFoo(T){ void foo(){writeln("1");} } // #1 > template TFoo(T : T[]) { void foo(){writeln("2");} } // #2 I don't have such problems because I am not smart enough to understand that syntax so I don't use it. :) I use template constraints (which

Re: Concurrency message passing

2021-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/17/21 11:36 AM, JG wrote: >>> Maybe message parsing isn't the >>> correct solution? I use message passing in many of my programs. > After being populated it should be passed to > the other thread and no references are kept. Then you simply cast to-and-from 'shared' and be happy with it. :

Re: Non-consistent implicit function template specializations

2021-08-18 Thread Ali Çehreli via Digitalmars-d-learn
On 8/18/21 4:10 AM, Rekel wrote: >> isArray!T && (isArray!(ElementType!T)) > > I tried looking into how isArray is defined. Like, does being able to > index mean it's an array, or are these only static &/or dynamic arrays? The definitions are in phobos/std/traits.d enum bool isArray(T) = is

<    3   4   5   6   7   8   9   10   11   12   >