Re: length's type.

2024-02-13 Thread Kevin Bailey via Digitalmars-d-learn
On Tuesday, 13 February 2024 at 23:57:12 UTC, Ivan Kazmenko wrote: I'd like to note that even C++20 onwards has `.ssize`, which is signed size. I do use lengths in arithmetic sometimes, and that leads to silent bugs currently. On the other hand, since going from 16 bits to 32 and then 64,

Re: length's type.

2024-02-09 Thread Kevin Bailey via Digitalmars-d-learn
On Friday, 9 February 2024 at 11:00:09 UTC, thinkunix wrote: First off I, I am just a beginner with D. I joined this list to try to learn more about the language not to but heads with experts. I'm sorry if you took my response that way. Hi thinkunix, I did interpret your post as

Re: length's type.

2024-02-08 Thread Kevin Bailey via Digitalmars-d-learn
Arafel, You're certainly correct. Priorities change. It used to be thought that backwards compatibility was the way to attract developers. But today the keyword is "safety". Apparently 2022 was the year of the C++ successor. Some features of D were mentioned in the discussion but D as a

Re: length's type.

2024-02-08 Thread Kevin Bailey via Digitalmars-d-learn
On Thursday, 8 February 2024 at 08:23:12 UTC, thinkunix wrote: I would never write code like this. By all means, please share with us how you would have written that just as elegantly but "correct". It would also break if the array 'something' had more than int.max elements. Then don't

Re: length's type.

2024-02-07 Thread Kevin Bailey via Digitalmars-d-learn
On Wednesday, 7 February 2024 at 20:13:40 UTC, Gary Chike wrote: On Wednesday, 7 February 2024 at 20:08:24 UTC, Gary Chike wrote: On Wednesday, 7 February 2024 at 19:32:56 UTC, Gary Chike wrote: double c = (double)sumArray(a, aLength) / aLength; If I don't cast explicitly: `double c =

Re: Changing behavior of associative array

2023-12-16 Thread Kevin Bailey via Digitalmars-d-learn
On Saturday, 16 December 2023 at 22:44:16 UTC, Dennis wrote: That's because `m[f] = 1` initializes the associative array to something non-null. If you pass a `null` AA to a function which adds things, the caller will still have a null pointers. You can initialize a non-null empty AA like

Changing behavior of associative array

2023-12-16 Thread Kevin Bailey via Digitalmars-d-learn
Perhaps someone can help solve this mystery. I have a sample program that adds structs to an associative array (as keys) in a subroutine. The program passes the array by reference as expected (below). My real program does the same thing and yet the associative array is passed by value. Thus

Re: Permutations of array (slice) ?

2023-12-15 Thread Kevin Bailey via Digitalmars-d-learn
On Wednesday, 13 December 2023 at 20:28:11 UTC, Nick Treleaven wrote: This code works for me. (The last 20+ lines are `b`): ```d import std; void main() { char[] as = replicate(['a'], 5); as[0] = 'b'; foreach (perm; as.byChar.permutations) writeln(perm); } ``` Try it on

Re: Permutations of array (slice) ?

2023-12-12 Thread Kevin Bailey via Digitalmars-d-learn
On Tuesday, 12 December 2023 at 15:20:23 UTC, Paul Backus wrote: Because of autodecoding [1], slices of char and wchar are treated by Phobos as bidirectional ranges of dchar. (They are not random-access ranges because UTF-8 and UTF-16 are variable-length encodings.) To work around this,

Permutations of array (slice) ?

2023-12-12 Thread Kevin Bailey via Digitalmars-d-learn
Could I get some help with a trivial question, I'm sure? How do pass a slice as a range? This code: import std.algorithm; // permutations import std.range; // replicate import std.stdio; // writeln int main() { char[] as = replicate(['a'], 5); as[0] = 'b'; foreach (perm;

Re: Find in assoc array then iterate

2022-10-22 Thread Kevin Bailey via Digitalmars-d-learn
ah, I knew that I could iterate over byKey, but I didn't know that it was a tangible thing, that you could hold in your hand. This should be fine, and I'll use your template trick for passing it to a function. Thanks Paul and Ali!

Re: Find in assoc array then iterate

2022-10-22 Thread Kevin Bailey via Digitalmars-d-learn
Siarhei, Thanks for this possibility. I didn't know D had "pointers" like this. Unfortunately, it appears that you can't pick up where you left off with that pointer? You have to re-scan forward? bachmeier, I didn't reply to Steven's question; I replied to his claim that there was no

Re: Find in assoc array then iterate

2022-10-21 Thread Kevin Bailey via Digitalmars-d-learn
Hi Sergey, While the unordered map doesn't guarantee an ordering (since its contents are hashed), the order should remain static if you don't insert or delete. Hi JG, Thanks for the red-black tree reference. I'll read up on it in case I need it but I'd prefer to use the built-in O(1) hash

Find in assoc array then iterate

2022-10-21 Thread Kevin Bailey via Digitalmars-d-learn
I'm trying to do this equivalent C++: unordered_map map; for (auto i = map.find(something); i != map.end(); ++i) ...do something with i... in D, but obviously with an associative array. It seems that it's quite easy to iterate through the whole "map", but not start from the

Re: Why are structs and classes so different?

2022-05-17 Thread Kevin Bailey via Digitalmars-d-learn
Hi again Ali! On Monday, 16 May 2022 at 21:58:09 UTC, Ali Çehreli wrote: I for one misunderstood you. I really thought you were arguing that struct and class should be the same. To be specific: - My *primary* concern is that: Foo foo; is undefined behavior waiting to happen, that I can't

Re: Why are structs and classes so different?

2022-05-16 Thread Kevin Bailey via Digitalmars-d-learn
On Monday, 16 May 2022 at 19:06:01 UTC, Alain De Vos wrote: A new syntax like "*" should introduce something new. If it's not needed for classes why introduce it. Hi Alain! I have to sympathize with Johan. If you see: Foo foo = get_foo(); call_function(foo); can 'foo' change in

Re: Why are structs and classes so different?

2022-05-16 Thread Kevin Bailey via Digitalmars-d-learn
Great responses, everyone. I'll try to address all of them. Mike, I know the rules. I was asking, "Why is it this way? Why was it designed this way? What bad thing happens the other way?" When I think about most things in D, I can at least think of a reason, even if I don't agree with it. But

Re: Why are structs and classes so different?

2022-05-15 Thread Kevin Bailey via Digitalmars-d-learn
Hi Mike (and Guillaume, since you posted the same link), Thanks for the long explanation. I've been programming in C++ full time for 32 years, so I'm familiar with slicing. It doesn't look to me like there's a concern here. There seem to be a couple different questions here. I suspect that

Why are structs and classes so different?

2022-05-15 Thread Kevin Bailey via Digitalmars-d-learn
I've done some scripting in D over the years but I never dug into D until recently. I'm going through Learning D and I was reminded that structs and classes are so different. - struct methods are non-virtual while class methods are virtual - Thus, structs can't inherit, because how would you

Template arg deduction

2021-07-07 Thread Kevin Bailey via Digitalmars-d-learn
I'm trying to use some fairly simple template argument deduction, but maybe I'm not getting the syntax correct. C++ doesn't event blink at something like this, but D is giving me: temptest.d(18): Error: template temptest.func cannot deduce function from argument types !()(bar), candidates

Re: Trying to search a range

2021-07-02 Thread Kevin Bailey via Digitalmars-d-learn
On Saturday, 3 July 2021 at 01:15:20 UTC, Paul Backus wrote: The error message here is actually telling you exactly what the problem is: `findSplit` requires a forward range, but the range returned by `File.byLine` is not a forward range, just an input range. Hey Paul, Thanks for the

Trying to search a range

2021-07-02 Thread Kevin Bailey via Digitalmars-d-learn
Sorry if this is too simple of a question, but I'm trying to simply find a blank line in a file. I'm doing something like: ``` auto file = File("somefile"); auto r = file.byLine(); auto tup = r.findSplit(""); // tup[0] would contain the range before the line, // tup[2] would contain the range

q about slices, strings

2019-07-13 Thread Kevin Bailey via Digitalmars-d-learn
I saw on the dlang.org homepage a sample of code that looks a little strange to me: int[char[2]] aa; auto arr = "ABBBA"; foreach (i; 0 .. arr.length - 1) aa[arr[i .. $][0 .. 2]]++; which I changed to: aa[arr[i .. i+2][0 .. 2]]++; in the hopes of only doing: