Re: What the hell is wrong with D?
On Saturday, 23 September 2017 at 20:43:36 UTC, Patrick Schluter wrote: So I checked for all the languages listed: C, C#, Java, Javascript, C++, PHP, Perl and D. All have the same order of precedence except, as always the abomination of all languages: C++ (kill it with fire). C++ is the only language that has the ternary operator have the same precedence than the assignment operators. This means a>=5?b=100:b=200; will compile in C++ but not in all the other languages. That's one reason why it irritates me when people continuously refer to C and C++ as if it was the same thing (yes I mean you Walter and Andrei). Even PHP and Perl got it right, isn't that testament of poor taste Bjarne?. :-) It's not quite as big of a deal as it seems because of the RTL associativity for both of them but still a very weird thing at face value.
Re: What the hell is wrong with D?
On Tuesday, 19 September 2017 at 19:16:05 UTC, EntangledQuanta wrote: [snip] I'm just glad there is at least one sane person that decided to chime in... was quite surprised actually. I find it quite pathetic when someone tries to justify a wrong by pointing to other wrongs. It takes away all credibility that they have. I have no doubt that had someone thought to propose addressing this when the language was new it would have been seriously considered and likely accepted (given how frequently this causes bugs). D tried to fix a lot of behavior from C that was bug prone but it didn't catch everything. If you want to help, I suggest trying to come up with a DIP that addresses it while being conscious of how to avoid breaking an enormous amount of code. I suspect it's a hard and maybe impossible problem but if you are up for the challenge I'm sure your efforts would be welcome.
Re: What the hell is wrong with D?
On Tuesday, 19 September 2017 at 18:17:47 UTC, jmh530 wrote: On Tuesday, 19 September 2017 at 17:40:20 UTC, EntangledQuanta wrote: Thanks for wasting some of my life... Just curious about who will justify the behavior and what excuses they will give. Pretty sure it would be exactly the same thing in C... It is (and Java and C# and pretty much every other C style language though the nicer implicit conversion rules means it gets caught more easily). It is a big source of programmer mistakes. It comes up frequently in PVS Studio's open source analysis write ups.
Re: Ranges suck!
On Thursday, 14 September 2017 at 23:53:20 UTC, Your name wrote: Every time I go to use something like strip it bitches and gives me errors. Why can't I simply do somestring.strip("\n")??? import std.string would be the likely strip yet it takes a range and somestring, for some retarded reason, isn't a range. strip isn't the only function that does this. Who ever implemented ranges the way they did needs to get their head checked! It's not really a range issue. It's that there are two strips. One in std.string and one in std.algorithm. The latter which lets you define what to strip rather than just whitespace is what you are looking for and works as you've written. The former is there for legacy reasons and we can hopefully get rid of it in the future to avoid this confusion. I'd also say that you don't seem to be grasping a pretty fundamental D concept yet. std.string.strip doesn't take two arguments, it takes one argument. The first set of parentheses is the template argument which is inferred from the regular argument using IFTI. [snip] Ok, so I know what your saying "Oh, but strip("\n") should be strip()! Your a moron RTFM!" But why then force me to strip for nothing? Why not pay me? e.g., let me strip for something like strip("x")? strip()! isn't valid syntax. If you want to strip all whitespace you can use std.string.strip (e.g., somestring.strip()). If you want to strip "x" you can use std.algorithm.strip (e.g., somestring.strip("x")). Pretty much like any other language minus the double function mess. Oh, chomp? Is that the function I'm suppose to use? Seriously? Was the D lib written by someone with a pacman fetish? chomp comes to D by way of perl. I don't know whether or not Larry Wall is into pacman or not.
Re: Chain a range of ranges?
On Tuesday, 17 January 2017 at 03:21:39 UTC, Yuxuan Shui wrote: The built in chain seems to only be able to chain a fixed number of ranges, is there a way to chain a range/array of ranges? See std.algorithm.iteration.joiner
Re: D code optimization
On Thursday, 22 September 2016 at 16:09:49 UTC, Sandu wrote: It is often being claimed that D is at least as fast as C++. Now, I am fairly new to D. But, here is an example where I want to see how can this be made possible. So far my C++ code compiles in ~850 ms. While my D code runs in about 2.1 seconds. [snip] Just a small tip that applies to both D and C++ in that code. You can use a static array rather than a dynamically allocated array in the loop (enum n = 252; then double[n+1] call; in D). You can also use "double[n+1] call = void;" to mimic C++'s behavior of uninitialized memory. Use GDC or LDC when doing performance related work as they generate faster code typically. I'd be surprised if the C++ and D code asm wasn't nearly identical for a big chunk of this code when using GCC/GDC or Clang/LDC.
Re: Can vibe d leverage existing web technologies?
On Tuesday, 13 September 2016 at 23:45:18 UTC, Intersteller wrote: vibe.d does not have much lateral support as the most commons web technologies do. Can vibe.d leverage pre-existing techs such as php, ruby/rails, etc? Starting from scratch and having to build a robust and secure framework is really not the way to go. Sure. Just use res.write(executeShell(["php", "-f", "somephpscript.php"]).output); or something like that. But seriously, you probably don't want to do that. It's like asking if ruby on rails can leverage php. Sure, they can communicate over HTTP or whatever else they support but trying to execute PHP from within Rails or vice versa just isn't really all that beneficial.
Re: dlang.org using apache?
On Wednesday, 8 June 2016 at 14:41:55 UTC, Ola Fosheim Grøstad wrote: [snip] I like the "are we fast yet" websites that various project put up, displaying improvements over time. You mean like this? http://digger.k3.1azy.net/trend/
Re: iota access in foreach loop
On Saturday, 4 June 2016 at 18:55:09 UTC, Brad Anderson wrote: On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote: [...] Check out enumerate() in std.range; int counter = 5; foreach(i, el; enumerate(randomCover(iota(counter writeln("index: ", i, " element: ", el); index: 0 element: 3 index: 1 element: 1 index: 2 element: 0 index: 3 element: 2 index: 4 element: 4 How could I have forgotten the UFCS rox version... foreach(i, el; iota(counter).randomCover.enumerate)
Re: iota access in foreach loop
On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote: Hi all! Could you help me clearify why a iota can't be accessed with two arguments in a foreach loop? following tests show my problem: What does work: int[] ku = [0, 1, 2, 3, 4]; foreach(i, el; ku) writeln("index: ", i, " element: ", el); What does not work: counter = 5; foreach(i, el; iota(counter)) writeln("index: ", i, " element: ", el); Motivation: In real I want to have: counter = 5; foreach(i, el; randomCover(iota(counter))) writeln("index: ", i, " element: ", el); so, I could follow a random permutation. Maybe there is another simple way to achieve this? PS: needed imports: import std.random : randomCover; import std.range : iota; import std.stdio : writeln; Check out enumerate() in std.range; int counter = 5; foreach(i, el; enumerate(randomCover(iota(counter writeln("index: ", i, " element: ", el); index: 0 element: 3 index: 1 element: 1 index: 2 element: 0 index: 3 element: 2 index: 4 element: 4
Re: Speed of csvReader
On Thursday, 21 January 2016 at 22:13:38 UTC, Brad Anderson wrote: On Thursday, 21 January 2016 at 21:24:49 UTC, H. S. Teoh wrote: [...] What about wrapping the slices in a range-like interface that would unescape the quotes on demand? You could even set a flag on it during the initial pass to say the field has double quotes that need to be escaped so it doesn't need to take a per-pop performance hit checking for double quotes (that's probably a pretty minor boost, if any, though). Oh, you discussed range-based later. I should have finished reading before replying.
Re: Speed of csvReader
On Thursday, 21 January 2016 at 21:24:49 UTC, H. S. Teoh wrote: [snip] There are some limitations to this approach: while the current code does try to unwrap quoted values in the CSV, it does not correctly parse escaped double quotes ("") in the fields. This is because to process those values correctly we'd have to copy the field data into a new string and construct its interpreted value, which is slow. So I leave it as an exercise for the reader to implement (it's not hard, when the double double-quote sequence is detected, allocate a new string with the interpreted data instead of slicing the original data. Either that, or just unescape the quotes in the application code itself). What about wrapping the slices in a range-like interface that would unescape the quotes on demand? You could even set a flag on it during the initial pass to say the field has double quotes that need to be escaped so it doesn't need to take a per-pop performance hit checking for double quotes (that's probably a pretty minor boost, if any, though).
Re: Disabling GC in D
On Thursday, 21 January 2016 at 21:54:36 UTC, Dibyendu Majumdar wrote: Is there a way to disable GC in D? I am aware of the @nogc qualifier but I would like to completely disable GC for the whole app/library. Regards Dibyendu GC.disable(); This prevents the garbage collector from running but your program will still allocate using the GC's managed memory (use the command line switch -vgc to see all the allocation points). It will just never free memory.
Re: DUB config format: SDLang or JSON?
On Friday, 18 December 2015 at 22:30:00 UTC, Jakob Jenkov wrote: I am just looking at DUB and I can read that there are two config formats: SDLang and JSON. Which one is the "new" format? Which one is the "future" of DUB? SDLang is the new one. JSON will remain supported. Use whichever you like better.
Re: Palindromes
On Friday, 4 December 2015 at 00:50:17 UTC, Meta wrote: On Friday, 4 December 2015 at 00:26:23 UTC, Jim Barnett wrote: On Friday, 4 December 2015 at 00:23:45 UTC, Jim Barnett wrote: The `import` statement inside the `for`-loop kind of smells to me. Sorry, inside the `while` loop In D it's considered idiomatic, but it can also cause some very hard to detect errors from time to time... I hate it for this reason and never do it in my own code. Inside a while, I don't think, really matches the idiom's goals. By only sticking imports inside the code that makes use of them you can achieve (I've never measured it though) compilation performance improvements in code that then imports the containing module but never makes use of the code in question. Sticking the import in the middle of the code is just noisy though, you want it nearby with limited scope but otherwise out of the way.
Re: Password Storage
On Friday, 27 November 2015 at 00:17:34 UTC, brian wrote: [snip] Can the developers in the room confirm if this is the correct approach? Are there examples of betters ways of doing this? Regards Brian Botan has well thought out password hashing: https://github.com/etcimon/botan/wiki/Password-Hashing
Re: compatible types for chains of different lengths
On Tuesday, 17 November 2015 at 22:47:17 UTC, Jon D wrote: I'd like to chain several ranges and operate on them. However, if the chains are different lengths, the data type is different. This makes it hard to use in a general way. There is likely an alternate way to do this that I'm missing. [snip] Is there a different way to do this? --Jon One solution: import std.stdio; import std.range; import std.algorithm; void main(string[] args) { auto x1 = ["abc", "def", "ghi"]; auto x2 = ["jkl", "mno", "pqr"]; auto x3 = ["stu", "vwx", "yz"]; auto chain1 = chain(x1, (args.length > 1) ? x2 : []); auto chain2 = chain(x1, x2, (args.length > 1) ? x3 : []); chain1.joiner(", ").writeln; chain2.joiner(", ").writeln; }
Re: How to install DMD 64bit on Windows?
On Wednesday, 21 October 2015 at 18:50:08 UTC, Adam D. Ruppe wrote: Use the .exe installer and it will offer to download and install visual studio for you as part for its process. I don't know if that feature has made it into a release yet. I don't think Vc2015 is supported yet either in a released version of DMD. I could be mistaken on both of these. If you want to play it say just install VS2013 Community Edition then install DMD and everything should just work.
Re: 'strong types' a la boost
On Saturday, 14 March 2015 at 15:45:30 UTC, Charles Cooper wrote: I think I may have answered my own question. It seems std.typecon provides a facility for this. http://dlang.org/phobos/std_typecons.html#.Proxy http://dlang.org/phobos/std_typecons.html#.Typedef Is this the 'right' way to do things? It seems that Proxy is used as a mixin whereas Typedef is used to create standalone types. If memory serves me, Typedef was created to regain this exact feature after typedef was deprecated. typedef did this while its replacement, alias, did not and the realization that we had lost that capability led to the creation of Typedef.
Re: Beginner ?. Why does D suggest to learn java
On Thursday, 16 October 2014 at 22:26:51 UTC, RBfromME wrote: I'm a newbie to programming and have been looking into the D lang as a general purposing language to learn, yet the D overview indicates that java would be a better language to learn for your first programming language. Why? Looks like D is easier than Java... The Overview page is ancient and needs to be rewritten. The included example sieve program reflects this. It's almost C (you'd only need to make minor changes to 4 of the lines to make it build with gcc). I'd agree that C probably isn't a good first language. The overview also suggests learning BASIC first which also shows just how old the Overview is (where do you even get a BASIC compiler these days?). There are easier languages but modern, idiomatic D is perfectly approachable for beginners in my opinion. https://issues.dlang.org/show_bug.cgi?id=13624
Re: how to get the \uxxxx unicode code from a char
On Tuesday, 14 October 2014 at 20:05:07 UTC, Brad Anderson wrote: https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579 Oops. Linked the the parser section. I actually don't see any unicode escape encoder in here. Perhaps he meant the upcoming JSON module.
Re: how to get the \uxxxx unicode code from a char
On Tuesday, 14 October 2014 at 20:03:37 UTC, jicman wrote: On Tuesday, 14 October 2014 at 19:49:16 UTC, Sean Kelly wrote: On Tuesday, 14 October 2014 at 19:47:00 UTC, jicman wrote: Greetings. Imagine this code, char[] s = ABCabc; foreach (char c; s) { // how do I convert c to something an Unicode code? ie. \u. } I'd look at the JSON string encoder. JSON? What does JSON has to do with my basic D? :-) No thanks. :-) Sean's saying that the JSON encoder does the same thing so you can look there for how to do it. https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579
Re: how to get the \uxxxx unicode code from a char
On Tuesday, 14 October 2014 at 20:08:03 UTC, Brad Anderson wrote: On Tuesday, 14 October 2014 at 20:05:07 UTC, Brad Anderson wrote: https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579 Oops. Linked the the parser section. I actually don't see any unicode escape encoder in here. Perhaps he meant the upcoming JSON module. Here we go. https://github.com/s-ludwig/std_data_json/blob/4ecb90626055269f4897902404741f1173fb5e8e/source/stdx/data/json/generator.d#L451 Sönke's is pretty sophisticated. You could probably just use the non-surrogate supporting simple branch.
Re: Turn function into infinite range
On Monday, 29 September 2014 at 17:02:43 UTC, Martin Nowak wrote: Does anyone know a construct to turn a lambda into an infinite range. import std.random; unittest { Random gen; foreach(v; xxx!(() = uniform(0, 100, gen)).take(10)) writeln(v); } I though I've seen this around somewhere but can no longer find it. I can't find anything to do it. That seems weirdly absent. You can abuse recurrence to do it. Random gen; foreach(v; recurrence!((a, n) = uniform(0, 100, gen))(0).dropOne.take(10)) writeln(v);
Re: Using the delete Keyword /w GC
On Monday, 25 August 2014 at 17:10:11 UTC, Etienne wrote: People have been saying for quite a long time not to use the `delete` keyword on GC-allocated pointers. I've looked extensively through the code inside the engine and even made a few modifications on it/benchmarked it for weeks and I still can't see why it would be wrong. Wouldn't it help avoid collections and make a good hybrid of manual management/collected code? The free lists in the GC engine look quite convenient to use. Any ideas? delete was deprecated because it is memory unsafe (there may still be references to the memory). You can still use GC.free() to free the memory but it must only be used with care. I feel if you are managing delete yourself you might as well manage allocation yourself too and take pressure off the GC. If you are sure you have only one reference and GC.free() is safe, Unique was probably a better choice anyway.
Re: new error message in 2.066, type bool (const)
On Thursday, 21 August 2014 at 03:02:53 UTC, Paul D Anderson wrote: What changed? It ran okay with early beta versions, but not with the release. Paul It compiles in beta-5 but not beta-6. Is the list of changes in the beta testing wiki complete? None seem pertinent. monarch_dodra: Thanks for checking. I was trying to avoid tearing everything down. I was hoping someone would recognize the error. Looks like I'll have to chase it down. Paul https://github.com/CyberShadow/digger should be able to help find the exact commit. A reduced example would help us figure out what is going on though.
Re: Produce some COFF object with 2.066 ?
On Wednesday, 20 August 2014 at 23:56:23 UTC, Baz wrote: Hello, I've been very interested about the announce saying that DMD is able to produce COFF object files. Mostly because I'm thinking using some objects programmed in D in a software programmed in another lang, a bit like when statically linking a dll to a program but with an obj, to keep a nice monolithic executable. First thing: I've tried a simple thing: compile an exported function with the args myfile.d -c -ms32mscoff and dmd complains that -ms32mscoff is not a recognized switch. 32-bit COFF is only in git master currently. It'll be in 2.067 when that comes out. 64-bit COFF has been in dmd for quite some time now. You just have to have a copy of Visual Studio installed (the free Express edition should be fine) and compile with -m64. Second thing: If I understand well, it means that previously, to link D a object with a soft programmed in another lang was not possible because the OMF objs don't include everything (e.g the objs coming from other imported static libs) and that now it's faisable ? right ? They'd just have to both be OMF format if you wanted to statically link. If you had a DLL you could create an import library from the DLL and still link that just fine. Walter has a tool on Digital Mars to do it. Now you should be able to directly link to COFF libraries.
Re: core.thread.Fiber --- runtime stack overflow unlike goroutines
On Thursday, 14 August 2014 at 07:46:29 UTC, Carl Sturtivant wrote: The default size of the runtime stack for a Fiber is 4*PAGESIZE which is very small, and a quick test shows that a Fiber suffers a stack overflow that doesn't lead to a clean termination when this limit is exceeded. This makes it difficult to simulate deterministic alternation where the stack size needed is unpredictable because complex deterministic computations are going on inside Fibers. In contrast, the Go programming language's goroutines can extend their stacks as needed at runtime, and so can be used to simulate deterministic alternation without this limitation, and yet be initially executed with each having only a small stack size. There seems to be a claim that all that's needed to add D-routines (goroutines for D) is a scheduler and a Channel type, on top of Fiber. http://forum.dlang.org/thread/lphnen$1ml7$1...@digitalmars.com See the initial post, point 7., as well as supporting remarks in later replies. Am I missing something? Is there a clean and simple way to get Fiber to no longer suffer a stack overflow when implementing D-routines? Segmented stacks come with a cost. Rust abandoned them for reasons you can read about here: https://mail.mozilla.org/pipermail/rust-dev/2013-November/006314.html I believe Go has taken steps to help mitigate stack thrashing but I don't know if they have been successful yet.
Re: Appender is ... slow
On Thursday, 14 August 2014 at 19:10:18 UTC, Jonathan M Davis wrote: I've never really tried to benchmark it, but it was my understanding that the idea behind Appender was to use it to create the array when you do that via a lot of appending, and then you use it as a normal array and stop using Appender. It sounds like you're trying to use it as a way to manage reusing the array, and I have no idea how it works for that. But then again, I've never actually benchmarked it for just creating arrays via appending. I'd just assumed that it was faster than just using ~=, because that's what it's supposedly for. But maybe I just completely misunderstood what the point of Appender was. - Jonathan M Davis I too have trouble understanding what Appender does that supposedly makes it faster (at least from the documentation). My old, naive thought was that it was something like a linked list of fixed size arrays so that appends didn't have to move existing elements until you were done appending, at which point it would bake it into a regular dynamic array moving each element only once looking at the code it appeared to be nothing like that (an std::deque with a copy into a vector in c++ terms). Skimming the code it appears to be more focused on the much more basic ~= always reallocates performance problem. It seems it boils down to doing essentially this (someone feel free to correct me) in the form of an output range: auto a = /* some array */; auto b = a; a = a.array(); for(...) b.assumeSafeAppend() ~= /* element */; (assumeSafeAppend's documentation doesn't say whether or not it'll reallocate when capacity is exhausted, I assume it does).
Re: Need help with building dmd
On Thursday, 7 August 2014 at 01:15:36 UTC, Phil Lavoie wrote: [...] make release -fwin32 Here is the output: Error: can't read makefile 'win32' I'm building on Windows btw. Thanks, Phil Close. You need the extension, I believe. make -f win32.mak release
Re: Need help with building dmd
On Thursday, 7 August 2014 at 01:27:55 UTC, Phil Lavoie wrote: Nope, still not working, but thx. Hmm, are you in the src directory?
Re: Need help with building dmd
On Thursday, 7 August 2014 at 01:50:50 UTC, Phil Lavoie wrote: On Thursday, 7 August 2014 at 01:37:46 UTC, Brad Anderson wrote: On Thursday, 7 August 2014 at 01:27:55 UTC, Phil Lavoie wrote: Nope, still not working, but thx. Hmm, are you in the src directory? Yes and ls *.mak shows three makefiles: osmodel.mak posix.mak win32.mak ls? If you're not, I recommend using the cmd.exe terminal. optlink has had problems with using other terminals. You may want to look at this guide if you haven't already: http://wiki.dlang.org/Building_DMD
Re: Taking from infinite forward ranges
On Tuesday, 5 August 2014 at 01:23:19 UTC, Andrew Edwards wrote: Is there a way to take a bounded rage from a infinite forward range? Given the Fibonacci sequence: auto fib = recurrence!(a[n-1] + a[n-2])(1, 1); I can take the first n elements: take(fib, 10); But say I want all positive elements below 5 in value (there are eight such values [2, 8, 34, 144, 610, 2584, 10946, 46368]), how would I take them? Of course I could filter the range, leaving only positive values, and then take(fib, 8). But what if I didn't know there were 8, how could I take them from there filtered range? Currently I do this: foreach(e; fib) { if (e = val) break; // so something with e } or while((e = fib.front()) n) { // do something with e fib.popFront(); } Is there a better way? I'd use std.algorithm.until: void main() { import std.algorithm, std.range, std.stdio; auto fib_until_50k = recurrence!(a[n-1] + a[n-2])(1, 1) .until!(a = a 50_000); writeln(fib_until_50k); }
Re: Problem with trying sample from doc page
On Wednesday, 16 July 2014 at 15:44:03 UTC, Adam D. Ruppe wrote: I would just change all the longs to ints and it would probably work. Or all the longs to ints. It really should have been consistent in the docs, since the point of this is delegate vs function, not int vs long... https://github.com/D-Programming-Language/dlang.org/pull/615
Re: Regex match in for loop
On Tuesday, 15 July 2014 at 20:18:58 UTC, seany wrote: Consider this: import std.stdio, std.regex, std.array, std.algorithms ; void main(string args[]) { string[] greetings = [hello, hallo, hoi, salut]; regex r = regex(hello, g); for(short i = 0; i greetings.count(); i++) { auto m = match(greetings[i], r); } } To the best of my knowledge, declaring a variable inside a for loop is illegal, you can not delacre the same variable repeatedly over the iterations. There is nothing wrong with declaring a variable in a for loop. It's just limited to the scope inside the for loop so it's not useful if you need the variable after the for loop ends. Also just the declaration auto m; outside the for loop does not make sense either - auto needs an Right Hand Side expression. So what is the correct way of doing it? You can type out the return type. It can be a little tricky to determine sometimes though so you can also use typeof() like: typeof(match(greetings[i], r) m; to get the proper type. You should use matchFirst and matchAll instead of match and the g flag. It's more clear and easier to use. What are you trying to do in this bit of code? There may be a better overall structure.
Re: Generating Strings with Random Contents
On Monday, 14 July 2014 at 22:21:36 UTC, bearophile wrote: Nordlöw: Is there a natural way of generating/filling a string/wstring/dstring of a specific length with random contents? Do you mean something like this? import std.stdio, std.random, std.ascii, std.range, std.conv; string genRandomString(in size_t len) { return len .iota .map!(_ = lowercase[uniform(0, $)]) .text; } void main() { import std.stdio; 10.genRandomString.writeln; } Bye, bearophile Alternative: randomSample(lowercase, 10, lowercase.length).writeln;
Re: Generating Strings with Random Contents
On Monday, 14 July 2014 at 22:27:57 UTC, Brad Anderson wrote: Alternative: randomSample(lowercase, 10, lowercase.length).writeln; std.ascii should really be using std.encoding.AsciiString. Then that length wouldn't be necessary.
Re: Generating Strings with Random Contents
On Monday, 14 July 2014 at 22:32:25 UTC, bearophile wrote: Brad Anderson: Alternative: randomSample(lowercase, 10, lowercase.length).writeln; From randomSample docs: Selects a random subsample out of r, containing exactly n elements. The order of elements is the same as in the original range. Bye, bearophile Hmm, good catch. Not the behavior I expected.
Re: '!' and naming conventions
On Wednesday, 18 June 2014 at 21:58:48 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: Sent: Wednesday, June 18, 2014 at 11:02 PM From: Brad Anderson via Digitalmars-d-learn digitalmars-d-learn@puremagic.com To: digitalmars-d-learn@puremagic.com Subject: Re: '!' and naming conventions There is a style guide on the website: http://dlang.org/dstyle.html Personally I just consider this a Phobos contributor style guide and not like a PEP8 style guideline. It was written with the hope that it would be generally followed by the D community, and that's part of the reason that it specifically focuses on the API and not the formatting of the code itself. So, ideally, most D projects would follow it (particularly if they're being distributed publicly) so that we have consistency across the community (particularly with regards to how things are captitalized and whatnot), but by no means is it required that every D project follow it. It's up to every developer to choose how they want to go about writing their APIs. We're not fascists and don't require that all code out there be formatted in a specific way or that all APIs follow exact naming rules (we couldn't enforce that anyway). But still, I would hope that most public D librares would follow the naming guidelines in the D style guide. Now, for Phobos, it's required, and there are even a couple of formatting rules added to the end specifically for Phobos, but outside of official D projects, it's up to the developers of those projects to choose what they want to do. - Jonathan M Davis I think it's a pretty good basic style guide overall and I follow it quite a bit, mostly due to coincidence (it overlaps with my own style I've developed over the years quite a bit). Really, the main thing I do differently is I use all lowercase, underscored names for variables instead of camelcasing. I don't care for the look of camelcase so I only use it for globals and other infrequently used things where I want it to stand out a bit from my regular variables. What we really need is a D Idiom Guide but that's a much more difficult and controversial subject.
Re: Doing exercise from book, but I'm getting error with splitter
On Monday, 16 June 2014 at 16:38:15 UTC, Sanios wrote: snip And I'm getting this - Error: undefined identifier splitter It seems like std.string doesn't contain splitter. You can find the solution to this and other issues you may hit in the errata: http://erdani.com/tdpl/errata/
Re: zip with fieldTuple
On Friday, 6 June 2014 at 22:16:36 UTC, John wrote: So let's say I'm trying to create a really simple ORM. I have a struct: struct foo { int a; float b; } I can iterate over the struct elements with the traits FieldTypeTuple!foo, I can iterate over the the string that represents the elements I want to shove in the struct, but when I try to loop over *both* of these at the same time with zip(...) I get an error. Code: void main() { string test = 1,2.0; foreach (t, value; zip(FieldTypeTuple!foo, test.split(,))) { writeln(to!t(value)); } } Error: src\orm.d(13): Error: template std.range.zip does not match any function template declaration C:\D\dmd2\windows\bin\..\..\src\phobos\std\range.d(3808): Error: template std.range.zip cannot deduce template function from argument types !()((int, float),string[]) I get what the error message is saying, but I have no idea how to fix the code to do what I want. I tried to look up what the FieldTypeTuple actually returns but it's calling a method on the generic type T called tupleOf(), which I can't seem to find (in that file or as a general function on object). I'm not sure if it's actually a range? I assumed it would be a range of some kind, and each of the elements would have a supertype of something like 'type' since that's what they are. It could infer that now you have two ranges, one of 'type' and one of 'string'. If I'm able to foreach over two things, shouldn't I be able to foreach over the paired ranges with zip? It seems so simple... foreach-ing over a typetuple is very different from doing it over regular variables. The compiler basically expands the foreach into several blocks of code (without introducing scope, I believe). So you are mixing compile time values and runtime values in a weird way. Types can't be zipped up with runtime values (or zipped up at all without some extra work). One way to do what you want is to foreach over the typetuple and use the index to index into the runtime values like this: struct foo { int a; float b; } void main() { import std.range, std.traits, std.stdio, std.conv; string test = 1,2.0; auto test_split = test.split(,); foreach (i, T; FieldTypeTuple!foo) { writeln(to!T(test_split[i])); } }
Re: zip with fieldTuple
On Friday, 6 June 2014 at 23:18:49 UTC, John wrote: On Friday, 6 June 2014 at 22:27:38 UTC, bearophile wrote: John: I can iterate over the struct elements with the traits FieldTypeTuple!foo, In such iteration you are using a static foreach. Types are compile-time constructs in D. If you need run-time entities you need to get their typeinfo. I don't want to do that lookup at runtime though. Clearly my intent is to rewrite this foreach zip expression as: auto s = test.split(,); writeln(to!int(s[0])); writeln(to!float(s[1])); What I wrote in my reply expands to exactly that. It's an array indexing which is very cheap (an addition on a ptr, basically). I can't think of a way to make this any faster if test is a runtime value. If test were a compile time value you could write it in such a way that it distills down to: writeln(1); writeln(2.0); But I suspect that's not what you are going for because test is meant to come in at runtime. zip only works on run time values. So you can't zip a built-in typetuple of types with a range of values. Conceptually that is what I want to do though. I want to pair the type with the string that I'm going to convert. I'm not sure if it's actually a range? I assumed it would be a range of some kind, It's not a range. FieldTypeTuple returns a built-in typetuple that in this case is really a built-in of types, that are purely compile-time entities. I get that. and each of the elements would have a supertype of something like 'type' since that's what they are. They are types (and they aren't other things like uninstantiated templates that in D are another kind), but not even in our dreams there is a supertype for them :-) It could infer that now you have two ranges, one of 'type' and one of 'string'. Nope. If I'm able to foreach over two things, shouldn't I be able to foreach over the paired ranges with zip? It seems so simple... If you turn the built-in typetuple of types into an array or lazy range of typeinfo, then you can zip them. But I don't think this is a good idea. It's better to forget the zipping and use a static foreach on the types, using also an index, and use such index to access the second array of run time values. I had already considered a workaround like that, but it's just that. A workaround. You already do unrolling for templates, this isn't much different (at least conceptually). You could basically do exactly what you're describing in the library, no? Have zip loop over all the static/compile time fields (assuming you can separate them in the template from the runtime ranges), and index into (or pop range) the runtime ranges with length checks. The compiler would unroll the compile time ranges (or whatever you want to call them) creating essentially exactly what you've described, the other poster mentioned, and precisely what I put above. Is that not possible? You could do something kind of like what you are describing, yes, but you have to remember that compile time values are passed in as template parameters. It'd have to look something like: zipWithTypes!(FieldTypeTuple!foo)(test.split(,)) And the element type of the result would have to be something like Variant if it's going to be a range. You could use a Tuple too if you don't care about the result having a range interface. Or you could represent types in runtime code with enums or something, like bearophile was saying.
Re: how to get line number after readln
On Thursday, 5 June 2014 at 00:33:26 UTC, Ali Çehreli wrote: On 06/04/2014 05:05 PM, Robert Hathaway wrote: I've got a program that reads a text file line by line (using std.stdio readln()) Consider using byLine() instead. (Important: byLine uses an internal buffer for the line; so, don't forget to make a copy if you want to store the line for later use.) and I'd like to refer to the line number when I send a message to stderr upon finding a mis-formatted line. Is there a way to get the current line number? Of course, I could create a counter and increment it with each call to readln, but is there a cool way of doing this? Okay, call me lazy... just don't call me late for dinner! :-) Robert One cool way is a zipped sequence: import std.stdio; import std.range; void main() { foreach (i, line; zip(sequence!n, File(deneme.txt).byLine)) { writefln(%s: %s, i, line); } } Ali Once this[1] gets merged you'll be able to do this: foreach (lineNum, line; File(deneme.txt).byLine().enumerate(1)) writefln(%s: %s, lineNum, line); Which is a bit more clear about the intent. 1. https://github.com/D-Programming-Language/phobos/pull/1866
Re: DateTime custom string format
On Tuesday, 3 June 2014 at 18:22:59 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: Well, I would prefer to do it myself, but I obviously can't say that I wouldn't accept it if someone else did it and did a good job of it. The main problem however is that we need to come up with a good formatting scheme - that is the format of the custom time strings. What C has doesn't cut it, and what I proposed a while back turned out to be too complicated. Just for reference to Robert and others reading, here's Jonathan's old proposal: http://forum.dlang.org/post/mailman.1806.1324525352.24802.digitalmar...@puremagic.com
Re: How to get array length
On Thursday, 22 May 2014 at 23:22:44 UTC, kaz wrote: Is there a way to get the length of an array out of slice bracket in D? Tks. Just use .length: void main() { import std.stdio; auto a = new int[5]; auto b = a[]; writeln(a.length, , b.length); }