Re: avoid toLower in std.algorithm.sort compare alias
Am Sun, 22 Apr 2012 09:23:45 +0200 schrieb Jay Norwood j...@prismnet.com: On Sunday, 22 April 2012 at 06:26:42 UTC, Jonathan M Davis wrote: You can look at the code. It checks each of the characters in place. Unlike toLower, it doesn't need to generate a new string. But as far as the comparison goes, they're the same - hence that line in the docs. - Jonathan M Davis ok, I did look at the code just now, and I'll sleep better knowing that it doesn't do the whole string conversion. I misunderstood your pseudo-code to mean that two lower case strings were being created prior to the compare. However, icmp code does appear to call the toLower conversion on both characters without first comparing the characters for equality, which misses the chance to do a simple compare that would avoid the two calls. /- check for equality :) v cmp!a != b std.uni.toLower(a) std.uni.toLower(b)(r1, r2) -- Marco
Re: What am I doing wrong ?
Am Sun, 22 Apr 2012 23:47:20 +0200 schrieb SomeDude lovelyd...@mailmetrash.com: void main() { auto array = new Foo[10]; -- for(int i = array.length; i 1; i--) { array[i].x = i; } writeln(); foreach(Foo f; array) { write(f.x);} } throws core.exception.RangeError@bug(8): Range violation on the line with the arrow. What am I doing wrong ? You could also try: foreach_reverse(i, ref f; array) { f.x = i; } -- Marco
Re: Docs: Section on local variables
On 04/25/2012 06:10 PM, Andrej Mitrovic wrote: On 4/25/12, Stewart Gordonsmjg_1...@yahoo.com wrote: Even if it's left over from debugging, it looks silly, and might lead other people reading the code to believe something's wrong. There's about a million ways to make code unreadable, and nobody writes pitch-perfect code that has absolutely no leftover code or comments. And what if you're refactoring and you do multiple builds every couple of seconds? You add a variable, remove it, etc etc. Enabling this warning will just make for a noisy compiler. Keeping variables clean is the responsibility of the programmer and not the compiler. If it doesn't affect the semantics of code the compiler should shut up. Please don't turn the compiler into a reincarnation of Clippy. +1. Another thing: It might not be unused in every static code path. Even more important: template isInputRange(R) { enum bool isInputRange = is(typeof( { R r; // can define a range object if (r.empty) {} // can test for empty r.popFront(); // can invoke popFront() auto h = r.front; // can declare an unused variable }())); } Having these kinds of errors in the compiler would be a major PITA that butchers the language without any benefit for correct code. This should not be the responsibility of the compiler. It is not a good match for D.
Re: Pointer to variables in D
On 04/26/2012 04:43 AM, Victor Vicente de Carvalho wrote: Hi there, In c++ one can access a pointer to a class/struct variable using this semantic: struct C { int x; }; int C::* ptr = C::x; C foo; foo.*ptr = 10; assert(foo.x == 10); It is possible to do something like that on D? I've searched through the forum documentation but didn't found anything. Also, the reason of this is that I'm studying a way to map a POD structure based from a dynamic, data-driven structure. Something like get a JSON and map it to a structure automagically. There is a nicer way to do that in D? http://www.drdobbs.com/blogs/cpp/231600610
Re: Pointer to variables in D
This is the closest thing: --- struct C { int x; int* ptr() @property { return x; } } C foo; *foo.ptr = 10; assert(foo.x = 10); --- Now you can also do: struct C { int x; } int* ptr() @property { return x; } C foo; *foo.ptr = 10; assert(foo.x = 10); if you can't or don't want to change C.
Re: Pointer to variables in D
Now you can also do: struct C { int x; } int* ptr() @property { return x; } C foo; *foo.ptr = 10; assert(foo.x = 10); if you can't or don't want to change C. int* ptr() @property { return x; } should be int* ptr(ref C c) @property { return c.x; }
Re: Power of D
Am 26.04.2012 07:55, schrieb Era Scarecrow: Associative arrays? C++: #include map #include string mapstring, string m; Java: import java.util.*; MapString, String map = new HashMapString, String(); D: string[string] map (Don't know the other two... sorry) -- Python: map = dict() # or map = {}
Re: Docs: Section on local variables
On 26/04/2012 08:26, Timon Gehr wrote: snip Another thing: It might not be unused in every static code path. One way to deal with this would be to do the checking before conditional compilation. That said, I've a feeling that mixin expansion might get in the way of this. Even more important: template isInputRange(R) { enum bool isInputRange = is(typeof( { R r; // can define a range object if (r.empty) {} // can test for empty r.popFront(); // can invoke popFront() auto h = r.front; // can declare an unused variable snip cast(void) r.front; Stewart.
Re: OT: Indent-sensitive languages again (was: Docs: Section on local variables)
Stewart Gordon: But one possible design for languages like these is to allow indentation to be either entirely spaces or entirely tabs, but not a mixture. This would also be a good way for linters for a variety of languages to behave. Among the arguments of the Python2.6 interpreter there is also: -t : issue warnings about inconsistent tab usage (-tt: issue errors) inconsistent tab usage means mixing tabs and spaces. Bye, bearophile
Re: Docs: Section on local variables
Timon Gehr: Andrej Mitrovic: Keeping variables clean is the responsibility of the programmer and not the compiler. If it doesn't affect the semantics of code the compiler should shut up. Please don't turn the compiler into a reincarnation of Clippy. +1. I think currently the D compiler doesn't shut up in some cases. Comparing the unused variable warning with Clippy is not good. Clippy gives suggestions, while here the compiler is giving something more like an error message. Another thing: It might not be unused in every static code path. Even more important: template isInputRange(R) { enum bool isInputRange = is(typeof( { R r; // can define a range object if (r.empty) {} // can test for empty r.popFront(); // can invoke popFront() auto h = r.front; // can declare an unused variable }())); } If the unused variable is a warning, and I use -wi that code compiles. The warning for unused variables helps me clean up my C code and has avoided me more than one bug. So I'd like this optional warning in the D front-end. I'd even like a warning for variables assigned and then later never read again. Bye, bearophile
Re: Power of D
On Thursday, 26 April 2012 at 10:50:49 UTC, David wrote: Am 26.04.2012 07:55, schrieb Era Scarecrow: Associative arrays? C++: #include map #include string mapstring, string m; Java: import java.util.*; MapString, String map = new HashMapString, String(); D: string[string] map (Don't know the other two... sorry) -- Python: map = dict() # or map = {} I think that many D powerful features are also easily done in Python or have easy to use equivalents, thanks to built-in dictionaries, list comprehensions, eval, etc. and so many available libraries. Albeit at the price of a slw execution comparing to D (unless you can utilize native extensions).
Re: Power of D
On Thursday, 26 April 2012 at 12:30:02 UTC, Nicolas Sicard wrote: I think that many D powerful features are also easily done in Python or have easy to use equivalents, thanks to built-in dictionaries, list comprehensions, eval, etc. and so many available libraries. Albeit at the price of a slw execution comparing to D (unless you can utilize native extensions). Heavily used features on a certain scale or larger _should_ be built into the language. C++ added new/delete for memory management, but didn't give you any good containers; Although the STL is there (Honestly without watching a good explanation of how the STL is suppose to work, I got totally lost, and nothing made sense). Honestly dealing with the issues of C++ templates, syntax and macros makes me feel like I'm driving with square wheels (It's a bumpy ride). To quote Adam Savage (Mythbusters) Square wheels are stupid. Unfortunately something in my brain makes learning unfamiliar languages that don't follow the structured syntax similar to C/Java/C++/D. I get utterly lost and my head as feels like it's dividing by zero.
Re: [Kinda OT] httpd permissions
Am Thu, 26 Apr 2012 05:44:59 +0200 schrieb Nathan M. Swan nathanms...@gmail.com: Have you checked that your web server has write access to /Users/nathanmswan/Sites/ ? Yes, it works now, thanks! NMS P.S. Sorry this might be in the wrong forum, but now I can advertise my homepage as index.d instead of compiling it and having it be index.cgi D is not a virtual machine language. You have to compile your code to execute it. (In case that was unclear.) As for the extension, that is probably a configuration option of your web server. Search for CGI extensions or .cgi and add .d there. -- Marco
Re: Docs: Section on local variables
On 26/04/2012 08:26, Timon Gehr wrote: snip template isInputRange(R) { enum bool isInputRange = is(typeof( { R r; // can define a range object if (r.empty) {} // can test for empty r.popFront(); // can invoke popFront() auto h = r.front; // can declare an unused variable }())); } snip This is indeed a blocker for fixing it to work according to the current spec. I've just filed http://d.puremagic.com/issues/show_bug.cgi?id=7989 to address it. Stewart.
Re: [Kinda OT] httpd permissions
On 26/04/12 15:52, Marco Leise wrote: Am Thu, 26 Apr 2012 05:44:59 +0200 schrieb Nathan M. Swannathanms...@gmail.com: Have you checked that your web server has write access to /Users/nathanmswan/Sites/ ? Yes, it works now, thanks! NMS P.S. Sorry this might be in the wrong forum, but now I can advertise my homepage as index.d instead of compiling it and having it be index.cgi D is not a virtual machine language. You have to compile your code to execute it. (In case that was unclear.) ... don't see why you shouldn't use rdmd. As for the extension, that is probably a configuration option of your web server. Search for CGI extensions or .cgi and add .d there. Isn't it considered bad practice in modern web design to have _any_ file extension visible? Or at least any file extension that hints at the underlying software. If nothing else, it's bad for future-proofing of URLs. What happens when you switch your site from D to D++ in the year 2025? :-)
Re: [Kinda OT] httpd permissions
Am Thu, 26 Apr 2012 16:12:05 +0200 schrieb Joseph Rushton Wakeling joseph.wakel...@webdrake.net: D is not a virtual machine language. You have to compile your code to execute it. (In case that was unclear.) ... don't see why you shouldn't use rdmd. Yes, rdmd is one of the tools that compile your code. It's just not like you call up a .php and it executes in a virtual machine _instead_ of _compiling_ and executing native code. -- Marco
Re: Power of D
On Wed, Apr 25, 2012 at 11:55 PM, Era Scarecrow rtcv...@yahoo.com wrote: On Wednesday, 25 April 2012 at 17:52:36 UTC, bioinfornatics wrote: i search some example of something easy (more easy) to do in D an not in another language if possible - D - C++ - D - Haskell - D - Java - D - python thanks a lot Associative arrays? C++: #include map #include string mapstring, string m; You actually want unordered_mapstring, string if you want the equivalent of D's string[string]. Regards, Brad Anderson
Re: Docs: Section on local variables
On 26/04/2012 15:05, Stewart Gordon wrote: snip http://d.puremagic.com/issues/show_bug.cgi?id=7989 From JMD: The fact that isInputRange and isForwardRange rely on declaring variables which aren't used being legal. It would be really annoying for unused local variables to be illegal when dealing with template constraint stuff like isInputRange and isForwardRange. Code would have to be needlessly contorted to deal with that fact, and you wouldn't ever get a good error about why the result of the template was false, because it would be part of a template constraint. IHMO, the very issue that this bug report brings up highlights a good reason why unused local variables should continue to be ignored by the compiler. (on 3960) I think that issue# 7989 is a great argument for why there shouldn't be any warnings or errors for unused variables. Such would needlessly make writing template constraints harder. Since this is relevant to both issues, I'll continue the discussion here. I can begin to see why it makes errors for unused variables a bad idea. But why no warnings? Obviously the user wouldn't like to see warnings thrown at them when they try using templates with such constraints. But: - The average programmer is, the vast majority of the time, not writing template constraints, but trying to write bug-free application code. - A quality compiler would swallow warnings generated by the content of IsExpressions, just as it already swallows errors generated by them - the only difference being that warnings don't cause the IsExpression to return false. Stewart.
Re: Docs: Section on local variables
On Thu, 26 Apr 2012 14:46:38 -0400, Stewart Gordon smjg_1...@yahoo.com wrote: I can begin to see why it makes errors for unused variables a bad idea. But why no warnings? Obviously the user wouldn't like to see warnings thrown at them when they try using templates with such constraints. But: - The average programmer is, the vast majority of the time, not writing template constraints, but trying to write bug-free application code. - A quality compiler would swallow warnings generated by the content of IsExpressions, just as it already swallows errors generated by them - the only difference being that warnings don't cause the IsExpression to return false. I think the mechanism is highly desired, but gets in the way in a select few situations. The best answer IMO is to disable that mechanism when it's not desired. It's then an opt-out mechanism that doesn't require instrumenting most code. Some ideas: pragma(used) int x; @used int x; -Steve
Re: Help with C struct by value on OSX 64bits
On Wednesday, 25 April 2012 at 19:43:27 UTC, Jacob Carlborg wrote: Compile as a 32bit binary. For DMD add -m32 do the flags. For gcc add -arch i386. Thanks Jacob, that worked!!! simendsjo: I upvoted and commented the same day bro.
Re: Docs: Section on local variables
Jonathan M Davis: I don't even know the last time that I saw an unused variable left in code (except for on purpose in something like isInputRange). So if the compiler warns you of unused variables, this will not cause your code almost no warnings. No troubles for you. For uncommon situations like isInputRange a specific annotation solves the problem cleanly. I'd much prefer that warning about that sort of thing be left up to a lint-like tool. How many C/C++ programmers do you know that use lints? I think not enough. The Microsoft C++ compiler and Clang are adding more and more compile-time tests, replacing lints, this a trend D designers can't ignore. So saying leave it to lints it's almost like saying ignore the problem. Bye, bearophile
Re: Docs: Section on local variables
On 26/04/2012 22:52, bearophile wrote: snip For uncommon situations like isInputRange a specific annotation solves the problem cleanly. As does the compiler swallowing warnings in the content of an IsExpression as I already suggested. snip How many C/C++ programmers do you know that use lints? I think not enough. The Microsoft C++ compiler and Clang are adding more and more compile-time tests, replacing lints, this a trend D designers can't ignore. So saying leave it to lints it's almost like saying ignore the problem. I agree. http://dlang.org/overview.html under Who D is for: Programmers who routinely use lint or similar code analysis tools to eliminate bugs before the code is even compiled. My impression from this has been that D aims to eliminate (or at least minimise) the need to use lint-type tools, by making the code smells lint is made to catch illegal code and therefore caught by the compiler. Stewart.
Re: Docs: Section on local variables
On Thursday, April 26, 2012 23:52:48 bearophile wrote: Jonathan M Davis: I don't even know the last time that I saw an unused variable left in code (except for on purpose in something like isInputRange). So if the compiler warns you of unused variables, this will not cause your code almost no warnings. No troubles for you. For uncommon situations like isInputRange a specific annotation solves the problem cleanly. And I'd argue that we might as well save ourselves the trouble of having to deal with yet _another_ special annotation just so that we can have warnings about something which is generally a non-issue. - Jonathan M Davis
How to avoid crashing win32 app when stdout is unavailable and write is called?
So I've just realized that write() throws an exception if stdout is unavailable and it's called. It throws this: std.exception.ErrnoException@D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1164): (Bad file descriptor) This happens when an app has a WinMain and is built with -L-Subsystem:Windows. It usually works ok if it's run from within another app that redirects stdout, for example if I invoke the app from Scintilla I will get stdout printed to the screen, but if I invoke the app from the console then stdout isn't properly set and calling write throws an exception. So, how do I redirect write's stdout so it doesn't throw if it doesn't find a valid stdout handle? Basically I want to turn write/writef into do-nothing functions if stdout isn't there. I know ideally I should use logging (still waiting for std.log) but using write is a handy way for me to debug things in Scintilla.
Fix iota for unsigned reverse counting
Issue 7982 [1] is caused by iota.length() [2] not working properly with unsigned values where the begin is greater than the end (the larger value is subtracted from the smaller value causing it to wrap around). I asked Andrei in IRC if replacing: return unsigned((pastLast - current) / step); with: return unsigned(((cast(Signed!Value)pastLast) - (cast(Signed!Value)current)) / step); was a sane approach. Although it does seem to fix the problem (though I haven't tested thoroughly) Andrei said it wasn't sane. I'm not sure what would be a better approach so I'm asking for ideas here. (why does std.traits have unsigned() to take advantage of IFTI but no signed() equivalent?) [1] http://d.puremagic.com/issues/show_bug.cgi?id=7982 [2] https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L4383 Regards, Brad Anderson