Re: how to assign multiple variables at once by unpacking array?
On Saturday, 7 October 2023 at 07:31:45 UTC, mw wrote: https://stackoverflow.com/questions/47046850/is-there-any-way-to-assign-multiple-variable-at-once-with-dlang How to do this Python code in D: ``` s = "1 2 3" A,B,C = map(int, s.split(" ")) A,B,C (1, 2, 3) ``` Is there a better way (since 2017)? Ranges for the win! ``` int a,b,c; "1,2,3" .splitter(',') .zip(only(&a, &b, &c)) .each!(x => *x[1] = x[0].to!int); writeln(a, b, c); ```
Re: how to assign multiple variables at once by unpacking array?
On Sunday, 8 October 2023 at 07:44:04 UTC, Andrea Fontana wrote: ``` int a,b,c; "1,2,3" .splitter(',') .zip(only(&a, &b, &c)) .each!(x => *x[1] = x[0].to!int); writeln(a, b, c); ``` or: ``` int a,b,c; only(&a, &b, &c) .zip("1,2,3".splitter(',')) .each!(x => *x[0] = x[1].to!int); writeln(a, b, c); ```
Re: how to assign multiple variables at once by unpacking array?
On Saturday, 7 October 2023 at 17:23:40 UTC, ryuukk_ wrote: there was a DIP for tuple/deconstruction prior to that question, sadly nothing came out of it, I don't think it was formally submitted. and now the language is frozen... The DIP process is temporarily suspended, it may be modified. The priority for (most of) this year is fixing bugs. However language changes do happen, e.g. just this weekend switch can now declare a variable in the condition. Atila is working on a DIP for editions.
Re: Define a new custom operator in D Language.
On Monday, 2 October 2023 at 21:37:56 UTC, bachmeier wrote: On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote: I'm unable to see how the operator overloading would allow to define a new custom operator. And I don't expect that to change. This has come up many times. With a parameter that has a symbol-like name you can use it like `x.mod(y)`, which looks alright too.
Re: Type constraint
On Wednesday, 4 October 2023 at 01:46:42 UTC, Joel wrote: I think the if without static is still static, since it's part of the function name part, or so (outside of the curly bracket scope). You can't have regular if-statements inside templates. Regular if-statements are checked at runtime but templates only exist at compile-time. The way to write a *template constraint*—which is what you want, and uses the `if` keyword—is thus: ```d struct List(T) if(__traits(isIntegral, T)){ auto addUp() //(Add numbers) } } ``` Notice that the curly brace for the template comes after the template constraint, and that there are only 2 curly braces rather than the 3 from your example. If the statement in the template constraint (`__traits(isIntegral, T)`) evaluates false at compile-time, then you will get a compiler error saying that the instantiation doesn't match the template constraints.
Re: array setting : Whats going in here?
On Saturday, 7 October 2023 at 00:00:48 UTC, claptrap wrote: char[] foo; foo.length = 4; foo[] = 'a'; // ok sets all elements foo[] = "a"; // range error at runtime? foo[] = "ab"; // range error at runtime? So I meant to init with a char literal but accidently used double quotes. Should that even compile? Shouldn't the compiler at least complain when trying to init with "ab"? Even though you now have gotten answers, I still agree with you that there should be some kind of "warning" or suggestion like, did you mean to assign incompatible types? It could just check the element type and see if it matches the rhs type.
Re: how to assign multiple variables at once by unpacking array?
On Sunday, 8 October 2023 at 07:45:56 UTC, Andrea Fontana wrote: On Sunday, 8 October 2023 at 07:44:04 UTC, Andrea Fontana wrote: ``` int a,b,c; "1,2,3" .splitter(',') .zip(only(&a, &b, &c)) .each!(x => *x[1] = x[0].to!int); writeln(a, b, c); ``` or: ``` int a,b,c; only(&a, &b, &c) .zip("1,2,3".splitter(',')) .each!(x => *x[0] = x[1].to!int); writeln(a, b, c); ``` Nice.
Re: how to assign multiple variables at once by unpacking array?
On Saturday, 7 October 2023 at 16:12:47 UTC, mw wrote: Interesting: in terms of easy of coding, clarity and future maintenance, which one is superior? The one liner in Python, or your "solution" with dozen lines of code? BTW, is that a solution at all? Did it achieved what the original goal asked in the OP question? So, who should learn from whom? If you don't expect to do a single line of coding, there are many methods in D that can do this kind of thing (but at compile time). **Your snippet with struct and tupple:** ```d import std; struct MyVariables { int age, phone, country; } void main() { enum sep = ", "; enum str = "21, 3149474, 90"; enum arr = str.split(sep) .map!(x => x.to!int) .array//*/ ; alias MrSmith = AliasSeq!(arr[0], arr[1], arr[2]); auto mv = MyVariables(MrSmith); assert(mv == MyVariables(21, 3149474, 90)); } ``` and **worksheet example:** ```d import std; struct DATA(string str, T, size_t s) { enum title = str; T[s] data; } void main() { alias Columns = AliasSeq!("Stock Name", "PN Codes", "P.Prices"); alias T = AliasSeq!(string, int, double); alias Items = AliasSeq!(4, 8, 8); staticMapN!(3, DATA, Columns, T, Items) worksheet; // inputs first column: worksheet[0].data = ["capacitor", "transistor", "resistor", "varistor"]; // prints column titles: foreach(s; worksheet) s.title.writef!"%14s"; "=".repeat(42).writefln!"\n%-(%s%)"; // prints first column: foreach(name; worksheet[0].data) name.writefln!"%14s"; //... "=".repeat(42).writefln!"%-(%s%)"; }/* Prints: Stock Name PN Codes P.Prices == capacitor transistor resistor varistor == */ ``` By the way, in order for the above code snippet to work, the staticMapN() template implemented by Ali Çehreli, which is not in the standard library, is needed: ```d template staticMapN(size_t N, alias fun, args...) if(args.length % N == 0) { alias staticMapN = AliasSeq!(); static foreach (i; 0..args.length / N) { static if (N == 1) { staticMapN = AliasSeq!(staticMapN, fun!( args[i] ));} else static if (N == 2) { staticMapN = AliasSeq!(staticMapN, fun!( args[0..$ / N][i], args[$ / N..($ / N) * 2][i] ));} else static if (N == 3) { staticMapN = AliasSeq!(staticMapN, fun!( args[0..$ / N][i], args[$ / N..($ / N) * 2][i], args[($ / N) * 2..($ / N) * 3][i] ));} } } ``` SDB@79
Re: how to assign multiple variables at once by unpacking array?
On Monday, 9 October 2023 at 01:15:21 UTC, Salih Dincer wrote: the staticMapN() template implemented by Ali Çehreli, which is not in the standard library, is needed: It would be great if the unlimited version was added to std.meta. This template seeded/sprouted in here: https://forum.dlang.org/thread/vwxilrqgjqtvjrnjj...@forum.dlang.org SDB@79
Re: array setting : Whats going in here?
On Sunday, October 8, 2023 8:08:46 AM MDT Imperatorn via Digitalmars-d-learn wrote: > On Saturday, 7 October 2023 at 00:00:48 UTC, claptrap wrote: > > char[] foo; > > foo.length = 4; > > foo[] = 'a'; // ok sets all elements > > foo[] = "a"; // range error at runtime? > > foo[] = "ab"; // range error at runtime? > > > > So I meant to init with a char literal but accidently used > > double quotes. Should that even compile? Shouldn't the compiler > > at least complain when trying to init with "ab"? > > Even though you now have gotten answers, I still agree with you > that there should be some kind of "warning" or suggestion like, > did you mean to assign incompatible types? > > It could just check the element type and see if it matches the > rhs type. Except that in those examples, they _do_ match. It's perfectly valid to copy elements of a string to a char[]. It's just copying immutable(char) to char. The compiler would complain if it couldn't implicitly convert the element type in the array being assigned from to the element type in the array being assigned to. The problem here is simply that the lengths of the arrays don't match. And in general, the compiler has no way of knowing whether the lengths match, because the lengths are dynamic. In this particular case, it could figure it out if it did sufficient flow analysis, but that's the sort of thing that typically isn't done in D, because it gets to be expensive and tends to result in inconsistent behavior, because small changes to the code could drastically change what the compiler is able to figure out. If the lengths were static, then the compiler actually would complain. e.g. foo[0 .. 3] = bar[1 .. 2]; would result in a compilation error such as q.d(5): Error: mismatched array lengths 3 and 1 for assignment `foo[0..3] = bar[1..2]` So, the compiler will complain both if it can't implicitly convert the element types to make the assignment work and if it can statically see that the lengths of the arrays don't much. As such, I'm not sure that there's actually anything that the compiler could do here to catch the problem in the OP's case (at least not without doing code flow analysis, which isn't going to happen). - Jonathan M Davis
allocated object address as high as 46th bit (i.e in the 131072 GB range)
https://dlang.org/library/core/bitop/bsr.html I'm trying to find out allocated object's address' space: ``` import std.stdio; import core.bitop; void main() { const size_t ONE_G = 1 << 30; char[][128] ptrs; foreach (i, ref ptr; ptrs) { ptr = new char[ONE_G]; if (ptr is null) { break; } writeln(i, ": ", bsr(cast(size_t)ptr.ptr)); } } ``` I tried on a few 64-bit machines (all of them have less than 128GB memory), and the result are about all the same: ``` $ uname -m x86_64 $ ./bit_op 0: 46 1: 46 2: 46 3: 46 4: 46 5: 46 6: 46 7: 46 8: 46 9: 46 10: 46 Killed ``` What puzzled me is that the highest set bit of the allocated address are all 46! i.e in the in the 2^47 ~= 131072 GB range! I know this could be an OS thing, but just wonder if anyone can give me some explanation? Thanks.
Re: allocated object address as high as 46th bit (i.e in the 131072 GB range)
As far as I'm aware, no cpu that you can get ahold of support more than 48bit of address space at the hardware level. There is simply no reason at this time to support more, due to the fact that nobody has implemented anywhere near that maximum. Also worth noting, the address a block of memory is, has no relation to the hardware. A kernel will instruct the cpu to map it wherever it pleases per process.
Re: allocated object address as high as 46th bit (i.e in the 131072 GB range)
On Monday, 9 October 2023 at 05:57:47 UTC, Richard (Rikki) Andrew Cattermole wrote: As far as I'm aware, no cpu that you can get ahold of support more than 48bit of address space at the hardware level. There is simply no reason at this time to support more, due to the fact that nobody has implemented anywhere near that maximum. Also worth noting, the address a block of memory is, has no relation to the hardware. A kernel will instruct the cpu to map it wherever it pleases per process. Thanks for the info. I'm surprised that kernel set virtual space that high.