Re: Understanding the GC
On Wednesday, 30 January 2013 at 06:00:44 UTC, Jeremy DeHaan wrote: From what I understand, when an object is recovered by the GC, the destructor may or may not be called. Why is that? Is it for That's not quite correct. When the object is collected, its destructor will be called. But you have no control over when or if it will happen during runtime. More on this below in relation to your question about unreferenced objects. performace reasons? What about the destructors of objects that the original object contained? Are they called when the item finally get's taken care of by the GC, or when the object is originally recovered by the GC? Destructors of members will not be called when an object is collected. Only that of the object itself. But, there's no guarantee that any member references will still be valid when the object's destructor is called. See below for more. My other question is, at the end of the program will GC go through every object it knows still holds memory and call their destructors? My guess is that it does so that the GC ensures Yes. all memory is successfully released before the program ends. I just want to make sure since in http://dlang.org/class.html, it says, The garbage collector is not guaranteed to run the destructor for all unreferenced objects. What exactly is an unreferenced object? A referenced object is one that is still in use by the program. Example: auto mc = new MyClass; // Here, there is exactly one reference to the MyClass instance // and it is still active. // But if i do this... auto mc2 = mc; // Now there are 2 references to the same instance. // Set one to null. mc = null; // Now there is only one reference. Null it... mc2 = null; // Now all references to the original instance are invalid, // so the object is unreachable by the program, so it is an // unreferenced object. The GC can only collect objects that it can be sure are unreferenced. Sometimes it can't be sure, in which case that particular object won't be collected during run time. For the objects that are determined to be collectable, there's no way to guarantee that any one of them will be collected in any given collection cycle. Because of that, you cannot rely on the order of destruction. Consider: class Foo {} class Bar { Foo f; } Bar b = new Bar; b.f = new Foo; Given that b.f is the only reference to this Foo instance, then this instance of Foo will become unreferenced at the same time that the instance of Bar does. But, again, we cannot rely on the Bar instance being collected and destructed before the Foo instance. So if you have a Bar destructor that tries to access a member in f, then you would get a crash some of the time. Worse, what if there were more references to the Foo instance outside of Bar that haven't been released yet? If you tried to destroy f from the Bar destructor, or perhaps call some sort of cleanup method to release system resources that f controls, you'll get a crash somewhere else in the program when another bit of code tries to access the no longer valid reference. That would likely be a marathon debugging session, because most of the time you don't expect that sort of thing. Some other questions: At what point is the GC called to do it's thing while the program is running? Is it at certain intervals or is it when the program's memory reaches a certain point? My understanding is that the current implementation only runs collections when memory is allocated. Meaning, when you allocate a new object instance, or cause memory to be allocated via some built-in operations (on arrays, for example), the GC will check if anything needs to be collected and will do it at that time. I don't know if it's run on every allocation, or only when certain criteria or met, and I really don't care. That's an implementation detail. The D language itself does not specify any of that.
Re: Understanding the GC
The take-home point of all of this is that you shouldn't rely on destructors for resource deallocation. You could do it in by manually destructing objects when you are finished with them (via the destroy() method), but then you have to be extra careful about class members, ownership, order of destruction, and all of that. I find that it's much simpler if I give each of my objects a specific cleanup method that releases its own resources. When an object is no longer needed, I call its cleanup method and clear any references to it. This ensures that system resources or whatever are released in a predictable order and I don't run into odd crashes from trying to release something that's no longer valid. The GC takes care of the rest.
Re: Why is the 'protected' attribute considered useless?
On Wednesday, 30 January 2013 at 03:38:39 UTC, Chad Joan wrote: I've read more than once now that 'protected' is considered useless in D. Why is this? I'm not sure what articles you are referring to, but a couple of points it might think of: * Anything protected can be made public by derived classes, thus any protected member is not much more safe than public. * private and protected in D works at module scope, not class scope. You can modify private and protected members from anywhere in the module.
Re: Why is the 'protected' attribute considered useless?
On Wednesday, 30 January 2013 at 03:38:39 UTC, Chad Joan wrote: I've read more than once now that 'protected' is considered useless in D. Why is this? I've never heard that before. Where have you read that? Several people, including me, have said that 'package' is useless -- could that be what you mean? (There are many discussions in the Java community about how useless it is in Java, D copied the same failed design). It's also true that 'protected' is in practice not very useful in D, as long as bug 314 remains unfixed. But I wouldn't describe it as useless.
Re: try to compile githubs dmd-master zip with vstudio 2010
On Wednesday, 30 January 2013 at 05:39:03 UTC, dennis luehring wrote: Am 27.01.2013 15:08, schrieb Namespace: You mean the Visual Studio solution? I tried it also, but for me only the solution above works fine. I asked for that problem here: http://forum.dlang.org/thread/rzvaprvvgdtwrnoto...@forum.dlang.org?page=2#post-ehulzblzddasvyxncvdb:40forum.dlang.org can you add your win32 vc build findings also to the buld wiki? http://wiki.dlang.org/Building_DMD Maybe tonight. are the missing files generated by the make process? AFAIK yes.
Re: Understanding the GC
On Wednesday, 30 January 2013 at 08:15:15 UTC, Mike Parker wrote: On Wednesday, 30 January 2013 at 06:00:44 UTC, Jeremy DeHaan wrote: From what I understand, when an object is recovered by the GC, the destructor may or may not be called. Why is that? Is it for That's not quite correct. When the object is collected, its destructor will be called. But you have no control over when or if it will happen during runtime. More on this below in relation to your question about unreferenced objects. To add to that, you also have to keep in mind that when the program terminates (even legally), instead of running a *full* collect cycle, the program just leaves, and lets the OS clear any allocated memory. This is both faster, and safer. What this means is that while there is a guarantee that collection=destruction, there is no guarantee that actual collection will happen. If you absolutely must be sure that something allocated gets *destroyed*, either destroy it yourself via an explicit call, or bind it to a stack based RAII scheme, possibly with reference counting. performace reasons? What about the destructors of objects that the original object contained? Are they called when the item finally get's taken care of by the GC, or when the object is originally recovered by the GC? Destructors of members will not be called when an object is collected. Only that of the object itself. But, there's no guarantee that any member references will still be valid when the object's destructor is called. See below for more. Just to be clear, I suppose you (both) are talking about member references? EG: Nested classes? Destroying an object 100% guarantees its member destroyers are also called, outer to inner, first in first out, as part of the destruction process. The thing is that when you store a class reference then you call the destructor of the reference itself. References being glorified pointers, it basically means it does nothing. // struct S{} class A{} struct/class SA { S s; //destroying this means ~S gets called. A a; //destroying this means doing a = null. }
Re: Why is the 'protected' attribute considered useless?
On Wed, 30 Jan 2013 09:20:54 -, simendsjo simend...@gmail.com wrote: On Wednesday, 30 January 2013 at 03:38:39 UTC, Chad Joan wrote: I've read more than once now that 'protected' is considered useless in D. Why is this? ... * private and protected in D works at module scope, not class scope. You can modify private and protected members from anywhere in the module. I think this is the most common reason ppl think protected is useless, because it has no effect inside the module scope.. but by the same logic private is as useless, so.. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Re: Why is the 'protected' attribute considered useless?
On Wednesday, 30 January 2013 at 03:38:39 UTC, Chad Joan wrote: I've read more than once now that 'protected' is considered useless in D. Why is this? In my opinion this is because implementation inheritance is not idiomatic for D and lot of people may never encounter practical need to use one. Still needed when really needed though.
Re: Why is the 'protected' attribute considered useless?
Am 30.01.2013 04:38, schrieb Chad Joan: I've read more than once now that 'protected' is considered useless in D. Why is this? Tbh, I would consider everything else other than public useless. It's part of the user to use things according to the documentation and there are conrnercases where someone wants/needs to do something which was not intended to do, like accessing an internal value (I had this problem with std.parallelism at one point). You don't have to force the user to do everything correctly (according to you), it's part of the user to use your library etc. with care and not limit his possibilities.
Re: Is there a string remove method, that takes an index
On 2013-01-30 04:27, Ali Çehreli wrote: s = s[0..7] ~ s[8..$]; As with the other slicing approaches, it would be best to check first if s.length = i (with i = 8 in this case).
Re: Understanding the GC
On Wednesday, 30 January 2013 at 10:29:26 UTC, monarch_dodra wrote: On Wednesday, 30 January 2013 at 08:15:15 UTC, Mike Parker wrote: Destructors of members will not be called when an object is collected. Only that of the object itself. But, there's no guarantee that any member references will still be valid when the object's destructor is called. See below for more. Just to be clear, I suppose you (both) are talking about member references? EG: Nested classes? I found calling member class references as nested classes confusing - they are very different. Destroying an object 100% guarantees its member destroyers are also called, outer to inner, first in first out, as part of the destruction process. The thing is that when you store a class reference then you call the destructor of the reference itself. References being glorified pointers, it basically means it does nothing. Are you implying that in following code snippet: class A { ~this() { ... } } class B { A a; ~this() { ... } } destructor of A will be always called after B, so a member is always accessible in B's dtor?
Re: Understanding the GC
On Wednesday, 30 January 2013 at 11:57:01 UTC, Maxim Fomin wrote: On Wednesday, 30 January 2013 at 10:29:26 UTC, monarch_dodra wrote: On Wednesday, 30 January 2013 at 08:15:15 UTC, Mike Parker wrote: Destructors of members will not be called when an object is collected. Only that of the object itself. But, there's no guarantee that any member references will still be valid when the object's destructor is called. See below for more. Just to be clear, I suppose you (both) are talking about member references? EG: Nested classes? I found calling member class references as nested classes confusing - they are very different. Destroying an object 100% guarantees its member destroyers are also called, outer to inner, first in first out, as part of the destruction process. The thing is that when you store a class reference then you call the destructor of the reference itself. References being glorified pointers, it basically means it does nothing. Are you implying that in following code snippet: class A { ~this() { ... } } class B { A a; ~this() { ... } } destructor of A will be always called after B, so a member is always accessible in B's dtor? No. What gave you that idea? I said the reference a will be destroyed (set to null). The actual referenced class instance may or may not be destroyed by then. The class instance of A itself is not a member of B.
Re: Understanding the GC
On Wednesday, 30 January 2013 at 12:08:07 UTC, monarch_dodra wrote: On Wednesday, 30 January 2013 at 11:57:01 UTC, Maxim Fomin wrote: On Wednesday, 30 January 2013 at 10:29:26 UTC, monarch_dodra wrote: On Wednesday, 30 January 2013 at 08:15:15 UTC, Mike Parker wrote: Destructors of members will not be called when an object is collected. Only that of the object itself. But, there's no guarantee that any member references will still be valid when the object's destructor is called. See below for more. Just to be clear, I suppose you (both) are talking about member references? EG: Nested classes? I found calling member class references as nested classes confusing - they are very different. Destroying an object 100% guarantees its member destroyers are also called, outer to inner, first in first out, as part of the destruction process. The thing is that when you store a class reference then you call the destructor of the reference itself. References being glorified pointers, it basically means it does nothing. Are you implying that in following code snippet: class A { ~this() { ... } } class B { A a; ~this() { ... } } destructor of A will be always called after B, so a member is always accessible in B's dtor? No. What gave you that idea? I said the reference a will be destroyed (set to null). The actual referenced class instance may or may not be destroyed by then. The class instance of A itself is not a member of B. English is not native for me. Sometimes non-natives misunderstand the meaning of the words.
Re: Understanding the GC
On Wednesday, 30 January 2013 at 12:17:33 UTC, Maxim Fomin wrote: English is not native for me. Sometimes non-natives misunderstand the meaning of the words. My apologies.
Re: Tutorial on how to build DMD/druntime/phobos and docs from source?
On 29.01.2013 21:25, Philippe Sigaud wrote: On Mon, Jan 28, 2013 at 10:14 PM, H. S. Teoh hst...@quickfur.ath.cx wrote: On Mon, Jan 28, 2013 at 09:24:46PM +0100, Philippe Sigaud wrote: Besides the wiki, see: https://xtzgzorex.wordpress.com/2011/07/31/d-building-dmd-and-phobos-on-linux/ Would you kindly add this info to the wiki? :-) Where on the page? Ideally, integrate it into the current page structure. Or reorg the page as you see fit. OK, I integrated some explanations on dmd.conf. I also added /tools.git and explanations at the end. Hi all, this thread helped me much, but I ran into some trouble during bootstrapping dmd, because of some todos, not mentioned in the wiki. First I will give you a field report, explaining my way, I installed dmd from source (not really a shortstory ;-)) ..) and then, I hope, I will ask some not so silly questions. I use gentoo and installed dmd-2.60, because that's actually the last version in portage (the gentoo packagemanager). All went ok, I tested some little d-examples and then I wanted to get dmd-2.61 to run. Now the trouble begun: First I followed the wiki, tried to use the actual git version: dmd compiles, but the druntime and phobos don't do the same. Much warnings and some errors were shown. Ok, I'll go the same way with the last release: dmd.2.61.zip But mostly the same happened, some other warnings and errors, but the libraries won't compile. It took me some time to realize, that it was dmd-2.60 that caused the errors: To compile the libraries out of 2.61 or git, one needs a recent compiler. Nice: I found it as binary in the linux directory in the root of 'dmd.2.61.zip'. Luck: My PATH-Variable points to /usr/local/bin:/usr/bin:..., The portage version of dmd-2.60 was installed in /usr/bin, so I copied the 2.61-binaries to /usr/local/bin without need to delete the old version before. druntime and phobos now compiled without errors, but even the tools made some trouble again. The phobos- and druntime-sources were not found at the right places. This time there's even a reasonable hint, so I know, where the sources are searched for. After copying to the right path, now even the tools got compiled. Until now I'm not convinced, that the result of my work is a clean dmd v.2.62 (git) compiler environment or a mix out of v.2.60 (the gentoo-package), v.2.61 (out of the zip) and the git-sources and compilations itself. Why? There's a dmd.conf in /etc, which points to the libraries and include-files of version 2.60. There's another one in /usr/local/bin which I copied there together with the binaries I mentioned above and in the wiki is mentioned, to create a dmd.conf in /usr/src/d/dmd/src/ and it seems to me, that dmd looks at some default places to find what it needs. Now my questions / suggestions: At what places and in which order dmd searches for dmd.conf files? What files should be placed in what linux-directories? It can't be reasonable, to set the PATH-Variable to /usr/src/dmd/src, to get the latest dmd-compiler running, even so for Library- and Include-Paths. Whith next git-update, there will be a newly generated mix of versions again. So what files do one need to copy to the standard-directories of Linux, so that all functionality of dmd will be reachable for daily use? In the wiki it should be mentioned, that one needs a recent enough dmd-compiler, to get this stuff running, where to get it and with what steps to install it. An idea: Like in the dmd.2.61.zip, there could be a directory linux in the git-repository, with the needed bin-files in it. I would even appreciate, if there would be the whole compiler-environment with bins, libraries, includes and sources all of the same version, so one has a clean environment for bootstrapping dmd2 / druntime / phobos from git-sources. My problem was, that gentoo isn't able, to install .deb- or .rpm-files. That's why I wanted to create it from source, but whithout knowing what I know now, that was a bad task. I hope this report points out, what problems occur for people dealing new with dmd. It's not already a checklist, howto bootstrap dmd, but this report together with your knowledge of dmd, can help improve the wiki and the bootstrapping itself. Sorry, that it got so long, but leaving anything out wouldn't point out, what I wanted to say. Thanks all for your work on dmd. Maybe - and I hope so - I can help a bit in the future and do some of the work myself. Greetings, Timewulf
Sorted output from an associative array
Let's say i have an array: int[string] wordCount. How to print key:value pairs ordered by descending value? Or generally how to to store wordCount in an array of structs or type tuples for later sorting? In Python I would use something like this: sorted(wordCount.items(), key=lambda a: a[1], reverse=True).
Variadic constructor conflict
This code compiles fine: struct Vector(T, uint SIZE) { T[SIZE] vector; this(T value) { foreach (ref v; vector) v = value; } } alias Vector!(int, 3) Vec3i; but if I add a variadic constructor: struct Vector(T, uint SIZE) { [...] this(T...)(T values) if (values.length == SIZE) { foreach (i, v; values) vector[i] = v; } } alias Vector!(int, 3) Vec3i; I get: main.d(54): Error: template main.Vector!(int, 3).Vector.__ctor(T...)(T values) if (values.length == SIZE) conflicts with constructor main.Vector!(int, 3).Vector.this at main.d(46) main.d(111): Error: template instance main.Vector!(int, 3) error instantiating I think that should not happen because when there is a conflict call like Vec3i(3) the compiler must(?) use the specialized function...
Re: Sorted output from an associative array
FG: Let's say i have an array: int[string] wordCount. That's an associative array, and it's unsorted just like a Python dict. In Phobos there is a sorted tree, if you want, that keeps keys sorted. How to print key:value pairs ordered by descending value? There are various solutions. A simple solution is to use a foreach to create an array of 2-tuples (key,value), and then use a schwartzsort to sort just according to values. Or generally how to to store wordCount in an array of structs or type tuples for later sorting? You don't use type tuples for that, just Phobos tuples. This code is not fast because it appends (untested): Tuple!(string, int)[] items; foreach (k, v; wordCount) items ~= tuple(k, v); items.schwartzSort!(it = it[1], a b)(); In Python I would use something like this: sorted(wordCount.items(), key=lambda a: a[1], reverse=True). In Python you use key=itemgetter(1). And in Python2 you use iteritems() for that. Unfortunately in D there is no byPair() because that ties druntime with std.typecons... Bye, bearophile
Re: Sorted output from an associative array
On Wednesday, 30 January 2013 at 15:43:21 UTC, FG wrote: Let's say i have an array: int[string] wordCount. How to print key:value pairs ordered by descending value? Or generally how to to store wordCount in an array of structs or type tuples for later sorting? In Python I would use something like this: sorted(wordCount.items(), key=lambda a: a[1], reverse=True). Basically, you have to extract and sort a third party data structure. what you can do is extract only the keys though, and sort them according to a specific pred. Then once your keys are sorted, you can re-obtain the value from the original AA. Examplea addapted from TDPL: // void main(string[] args) { int[string] freqs = [a:1, b:3 , c:2]; // Print according to alphabet { string[] words = freqs.keys; sort(words); writeln(sorted by alphabet); foreach (word; words) writefln(%6u\t%s, freqs[word], word); writeln(); } // Print according to frequency { string[] words = freqs.keys; sort!((a, b) { return freqs[a] freqs[b]; })(words); writeln(sorted by frequency); foreach (word; words) writefln(%6u\t%s, freqs[word], word); writeln(); } } // sorted by alphabet 1 a 3 b 2 c sorted by frequency 3 b 2 c 1 a //
Re: Sorted output from an associative array
Tuple!(string, int)[] items; foreach (k, v; wordCount) items ~= tuple(k, v); items.schwartzSort!(it = it[1], a b)(); A little tested: import std.stdio, std.algorithm, std.typecons; void main() { uint[string] wordCount = [the:200, val:100, blue:1000]; auto items = new Tuple!(string, uint)[wordCount.length]; uint i = 0; foreach (k, v; wordCount) items[i++] = tuple(k, v); // avoided append items.schwartzSort!(it = it[1], a b)(); writeln(items); } Bye, bearophile
Fastq reader
Dear, I have wrote anither fastq reader: http://dpaste.dzfl.pl/2a7885dd Example of Fastq File-- @H8:C16L5ACXX:8:1101:1168:2103/1 TCTGAAGGCATGCTGCAATTGTGAATGGCAGAAATGT + ?@@DDDBDAFDF@4CFGICFHHECHEEBF;E@FFFG @H8:C16L5ACXX:8:1101:1223:2104/1 CTCACGTACTTTAGACAAGCGCAGTAGTGCT + @@;DD;?DHFFHFG9FAFCEGFGBFE1EFFGFGGHG9D* @H8:C16L5ACXX:8:1101:1166:2142/1 ATCTGGGAAGACGCCGCCGGGTTCAAA TCACCTTGGTCGGCATCGTCGATCCGC + ;=?D3C??)@:E1CDD)?B?B99BB8=8)8.=A888)56;9/2=?? A fastq file contains a: - identifier line - sequence line - comment line - quality line when you loop over Fastq with following code it will yied each turn the type ( identifier, sequence, comment, quality ) and the letter As: IDENTIFIER @ IDENTIFIER H IDENTIFIER 8 IDENTIFIER : IDENTIFIER C ... IDENTIFIER / IDENTIFIER 1 SEQUENCE T SEQUENCE C SEQUENCE T SEQUENCE G ... SEQUENCE A SEQUENCE T SEQUENCE G SEQUENCE T COMMENT + QUALITY ? QUALITY @ QUALITY @ ... QUALITY F QUALITY F QUALITY F QUALITY G So I would like by example compute in one pass - number of ACGT in sequence - count number of quality letter with @ This into distinct function as foreach( state, letter; file.Fastq ){ countBase( state, letter ); countQuality( state, letter, '@' ); } goal is to be able to add dynamicaly some function while reading file I do not want somethink like foreach( state, letter; file.Fastq ){ if( state == State.SEQUENCE ) stat[letter]++; if( state == State.QUALITY letter == '@' ) count++; } because in this case i can not to write some basics tool I think delegate i do not know if it is the way to go Thanks a lot, I hope my problem is enough clear
Re: Sorted output from an associative array
On Wednesday, 30 January 2013 at 16:22:37 UTC, monarch_dodra wrote: On Wednesday, 30 January 2013 at 15:43:21 UTC, FG wrote: Let's say i have an array: int[string] wordCount. How to print key:value pairs ordered by descending value? Or generally how to to store wordCount in an array of structs or type tuples for later sorting? In Python I would use something like this: sorted(wordCount.items(), key=lambda a: a[1], reverse=True). Basically, you have to extract and sort a third party data structure. what you can do is extract only the keys though, and sort them according to a specific pred. Then once your keys are sorted, you can re-obtain the value from the original AA. Yes, as mentioned, you can use schartzsort for better performance. Even then though, depending on the size of your values, it may be smarter to sort only the keys.
Re: Sorted output from an associative array
Thanks for showing me how to use tuples in this problem. Just for the record, the sort comparison should be reversed: a b.
Re: Adding more information to exceptions
On Tuesday, 29 January 2013 at 20:37:07 UTC, Andrej Mitrovic wrote: On 1/29/13, Vladimir Panteleev vladi...@thecybershadow.net wrote: foreach (lineNumber, line; lines) try numbers ~= to!int(line); catch (Exception e) throw new Exception(format(Error on line %d: %s, lineNumber, e.msg)); Of course, this has the problem that all information (except the message) from the original exception is lost. What I'd do is: catch (Exception e) { e.msg = format(My info here...\n%s, e.msg); throw e; } Ah, that's clever... thanks!
Re: Variadic constructor conflict
On 2013-01-30, 17:08, andrea9940 wrote: This code compiles fine: struct Vector(T, uint SIZE) { T[SIZE] vector; this(T value) { foreach (ref v; vector) v = value; } } alias Vector!(int, 3) Vec3i; but if I add a variadic constructor: struct Vector(T, uint SIZE) { [...] this(T...)(T values) if (values.length == SIZE) { foreach (i, v; values) vector[i] = v; } } alias Vector!(int, 3) Vec3i; I get: main.d(54): Error: template main.Vector!(int, 3).Vector.__ctor(T...)(T values) if (values.length == SIZE) conflicts with constructor main.Vector!(int, 3).Vector.this at main.d(46) main.d(111): Error: template instance main.Vector!(int, 3) error instantiating I think that should not happen because when there is a conflict call like Vec3i(3) the compiler must(?) use the specialized function... Known bug. For the moment, the workaround is to templatize all constructors: struct Vector(...) { this()(T value ) { ... -- Simen
Re: No mixin inside asm blocks
On Wed, Jan 30, 2013 at 4:01 PM, Ali Çehreli acehr...@yahoo.com wrote: A friend of mine is trying to figure out the D equivalent of using macros with asm blocks in C: #define NEXT() __asm__(jmp *%0::r((++ip)-jmp)); goto *ip-jmp D's asm blocks are very restrictive: mixins are not allowed. What do you do in D? Is it possible to enclose an asm statement into a mixin? mixin( asm { code to include });
Re: Variadic constructor conflict
Aw
Re: No mixin inside asm blocks
On 01/30/2013 11:08 AM, Philippe Sigaud wrote: On Wed, Jan 30, 2013 at 4:01 PM, Ali Çehreliacehr...@yahoo.com wrote: A friend of mine is trying to figure out the D equivalent of using macros with asm blocks in C: #define NEXT() __asm__(jmp *%0::r((++ip)-jmp)); goto *ip-jmp D's asm blocks are very restrictive: mixins are not allowed. What do you do in D? Is it possible to enclose an asm statement into a mixin? mixin( asm { code to include }); Thanks! It is almost there, except that one needs to use a compile-time macro expansion solution, which I am too lazy to try now. :) I am confident that the following program will work after doing that. (Of course also after replacing 'writeln' with 'mixin'.) import std.stdio; import std.regex; string expandNexts(string input) { enum e = ctRegex!(Next;, g); // NOTE: This is an example. Of course, the second string below should be // whatever the macro should be expanded as. return replace(input, e, cmp EAX,0;); } void main() { writeln (expandNexts(q{ asm { mov EAX, 1; Next; Next; Next; Next; Next; } })); } The program prints asm { mov EAX, 1; cmp EAX,0; cmp EAX,0; cmp EAX,0; cmp EAX,0; cmp EAX,0; } Ali
Re: Adding more information to exceptions
On Tuesday, 29 January 2013 at 21:53:46 UTC, Ali Çehreli wrote: On 01/29/2013 12:32 PM, Vladimir Panteleev wrote: I would like to add some information to any exceptions thrown inside the loop's body (e.g. whatever std.conv.to may throw), in our case the line number. Here is a RAII idea that takes advantage of exception chaining without directly using the 'next' parameter. It constructs a LineInfo object ready to throw in its destructor unless specifically told that it is not necessary anymore. import std.stdio; import std.string; import std.conv; void main() { auto lines = [ 42, 100, hello ]; int[] numbers; struct LineInfo { size_t lineNumber; bool allIsGood = false; ~this() { if (!allIsGood) { throw new Exception(format(Line number: %s, lineNumber)); } } } foreach (lineNumber, line; lines) { auto info = LineInfo(lineNumber); numbers ~= to!int(line); info.allIsGood = true; } } Ali umm, so why can't using next directly be valid?
Re: Adding more information to exceptions
On 01/30/2013 12:05 PM, Jesse Phillips wrote: On Tuesday, 29 January 2013 at 21:53:46 UTC, Ali Çehreli wrote: Here is a RAII idea that takes advantage of exception chaining without directly using the 'next' parameter. umm, so why can't using next directly be valid? The OP had quoted the documentation: http://dlang.org/phobos/object.html#.Exception The next parameter is used internally and should be always be null when passed by user code. Ali
Re: Adding more information to exceptions
On Wednesday, January 30, 2013 12:58:10 Ali Çehreli wrote: On 01/30/2013 12:05 PM, Jesse Phillips wrote: On Tuesday, 29 January 2013 at 21:53:46 UTC, Ali Çehreli wrote: Here is a RAII idea that takes advantage of exception chaining without directly using the 'next' parameter. umm, so why can't using next directly be valid? The OP had quoted the documentation: http://dlang.org/phobos/object.html#.Exception The next parameter is used internally and should be always be null when passed by user code. Really? That's a weird note. I don't see any reason for it to not be used by user code. I wonder why that note is there. - Jonathan M Davis
unnecessary casts
Is the compiler (dmd) fit enough to detect and avoid unnecessary casts? E.g. [code] void foo(T)(T num) { int f = cast(int) num; // ... } foo(42); // cast is unnecessary foo(4.2); // cast is necessary [/code] Or should I wrote everytime [code] void foo(T)(T num) { static if (is(T == int) || isImplicitlyConvertible!(T, int)) { int f = num; } else { int f = cast(int) num; } // ... } [/code] Until now I count on the compiler, that it detect this cases and avoid casts. But I'm not 100% sure, so I want to ask.
Re: unnecessary casts
On Wednesday, 30 January 2013 at 22:49:01 UTC, Namespace wrote: Is the compiler (dmd) fit enough to detect and avoid unnecessary casts? I think the most important casts most worth avoiding are the ones you write in the code (because they are a source of bugs), not the ones the compiler performs :-) Bye, bearophile
Looking for command for synchronization of threads
Background: I am implementing an iterative algorithm in parallel manner. The algorithm iteratively updates a matrix (2D grid) of data. So, I will divide the grid to different threads, which will work on it for single iteration. After each iteration, all threads should wait since next iteration depends on previous iteration. My issue: To achieve synchronization, I am looking for an equivalent of sync in Cilk or cudaEventSynchronize in CUDA. I saw synchronized, but was not sure, if that is the answer. Please help me. I will put that command at end of for loop and it will be executed once per iteration.
Re: unnecessary casts
On Wednesday, 30 January 2013 at 22:57:39 UTC, bearophile wrote: On Wednesday, 30 January 2013 at 22:49:01 UTC, Namespace wrote: Is the compiler (dmd) fit enough to detect and avoid unnecessary casts? I think the most important casts most worth avoiding are the ones you write in the code (because they are a source of bugs), not the ones the compiler performs :-) Bye, bearophile I'm talking about exactly these kind of casts. See my example.
Re: unnecessary casts
On Wednesday, January 30, 2013 23:49:00 Namespace wrote: Is the compiler (dmd) fit enough to detect and avoid unnecessary casts? E.g. [code] void foo(T)(T num) { int f = cast(int) num; // ... } foo(42); // cast is unnecessary foo(4.2); // cast is necessary [/code] Or should I wrote everytime [code] void foo(T)(T num) { static if (is(T == int) || isImplicitlyConvertible!(T, int)) { int f = num; } else { int f = cast(int) num; } // ... } [/code] Until now I count on the compiler, that it detect this cases and avoid casts. But I'm not 100% sure, so I want to ask. You'd have to look at the generated code to know for sure what it did, but it would be poor optimization to not strip the cast when casting from a value to its own type. And really, I'd argue that it's premature optimization to really worry about it in the first place. I'd argue that it's a bug if unnecessary casts are not optimized out, but unless you're searching for compiler bugs, there shouldn't be any reason to worry about it. - Jonathan M Davis
Re: Looking for command for synchronization of threads
On Jan 30, 2013, at 2:58 PM, Sparsh Mittal sparsh0mit...@gmail.com wrote: Background: I am implementing an iterative algorithm in parallel manner. The algorithm iteratively updates a matrix (2D grid) of data. So, I will divide the grid to different threads, which will work on it for single iteration. After each iteration, all threads should wait since next iteration depends on previous iteration. My issue: To achieve synchronization, I am looking for an equivalent of sync in Cilk or cudaEventSynchronize in CUDA. I saw synchronized, but was not sure, if that is the answer. Please help me. I will put that command at end of for loop and it will be executed once per iteration. I suggest looking at std.parallelism since it's designed for this kind of thing. That aside, all traditional synchronization methods are in core.sync. The equivalent of sync in Cylk would be core.sync.barrier.
Re: unnecessary casts
Namespace: I'm talking about exactly these kind of casts. See my example. I don't understand what you are trying to minimize. In both versions of your foo function you have 1 cast, so you aren't minimizing the number of casts you are writing in the code. Bye, bearophile
Re: Adding more information to exceptions
On 1/30/13, Jonathan M Davis jmdavisp...@gmx.com wrote: The next parameter is used internally and should be always be null when passed by user code. Really? That's a weird note. I don't see any reason for it to not be used by user code. I wonder why that note is there. It should probably be changed, TDPL shows how .next is used in user-code as well.
Re: Looking for command for synchronization of threads
I suggest looking at std.parallelism since it's designed for this kind of thing. That aside, all traditional synchronization methods are in core.sync. The equivalent of sync in Cylk would be core.sync.barrier. Thanks. I wrote this: #!/usr/bin/env rdmd import std.stdio; import std.concurrency; import std.algorithm; import core.sync.barrier; import core.thread; void sorter(Tid owner, shared(int)[] sliceToSort, int mynumber) { writefln(Came inside %s, mynumber); sort(sliceToSort); writefln(Going out of %s, mynumber); } void main() { shared numbers = [ 6, 5, 4, 3, 2, 1 ]; auto barrier = new Barrier(2); spawn(sorter, thisTid, numbers[0 .. $ / 2], 0); spawn(sorter, thisTid, numbers[$ / 2 .. $],1 ); writefln(Waiting for barrier in main); barrier.wait(); writeln(numbers); } It compiles but barrier does not get released. Can you please point out the fault. Pardon my mistake. I searched whole web, there are almost no examples of it online. I saw this: http://www.digitalmars.com/d/archives/digitalmars/D/bugs/Issue_9005_New_std.concurrency.spawn_should_allow_void_delegate_Args_shared_for_new_Tid_44426.html but it does not compile.
How does array assignment for different sized types work?
void main() { float[3] v1 = [1.0, 2.0, 3.0];// No error float[3] v = [1.0, 2.0, 3.0].dup; // Fails at runtime with error message } Why does the array assignment work when dup is not used. My understanding is that arrays, like classes, are references. So I declare v1 as a float[3] point it at a double[3] literal. Is v1 now a double[3] or a float[3]? Is there an implicit cast from double[3] to float[3]? The dup, gives a compile time error and if cast to float[] it gives a runtime error. This is all good, I just don't quite understand how the ref assignment is working... Thanks, Stewart
Re: How does array assignment for different sized types work?
On 2013-01-31 05:48, estew wrote: void main() { float[3] v1 = [1.0, 2.0, 3.0];// No error float[3] v = [1.0, 2.0, 3.0].dup; // Fails at runtime with error message } Why does the array assignment work when dup is not used. My understanding is that arrays, like classes, are references. There are dynamic arrays and static arrays. Dynamic arrays are reference types, static arrays are value types. You have declared a static array. http://dlang.org/arrays.html -- /Jacob Carlborg
Re: unnecessary casts
Le 30/01/2013 17:49, Namespace a écrit : Is the compiler (dmd) fit enough to detect and avoid unnecessary casts? E.g. [code] void foo(T)(T num) { int f = cast(int) num; // ... } foo(42); // cast is unnecessary foo(4.2); // cast is necessary [/code] Or should I wrote everytime [code] void foo(T)(T num) { static if (is(T == int) || isImplicitlyConvertible!(T, int)) { int f = num; } else { int f = cast(int) num; } // ... } [/code] Until now I count on the compiler, that it detect this cases and avoid casts. But I'm not 100% sure, so I want to ask. When the template gets instantiated, the compiler has to determine what cast(int) exactly means for type T ; if T is int, the obvious answer is to do nothing, so I don't see a problem. Just my guess.