code review based on what I learned from D
Hi, Recently while I was reviewing some swift code, a colleague left me the impression that I am the one with the bad habits and these were learned while coding in D. I still think that I proposed some changes to avoid some bugs but I was told that I am focusing on defensive programming and that is a bad thing. The first issue that I raised was this one: func renderCell(item: AnyObject, index: Int) { -fatalError("renderCell has not been implemented") + } where I proposed to make that method abstract or let's not remove the fatalError message because this method it should be never called. The second issue was this: +init(dataSource: WUPTableDataSource) { + +self.dataSource = dataSource +dataSource.tableView = tableView where I asked what happens if someone passes a dataSource that has a tableView set. I this class, there were set some events bind to the view and it was unclear what happened in that case and I proposed to add an assert to check if dataSource.tableView is not set before we set it. For both of these issues I was told that swift is not Java and if the situations that I described happens, you don't want to crash the user app, because this will make the user unhappy. Those things are for me, good habits that I do when I am programming with D. What do you think? and if I had bad ideas with those issues, what I can do to improve my skills? thanks, Bogdan
Re: Switching rows with columns
On Saturday, 4 July 2015 at 16:29:44 UTC, Marc Schütz wrote: Try std.range.transposed: http://dlang.org/phobos/std_range.html#.transposed Does this work with user-defined types? I defined two structs that implement the InputRange(possibly ForwardRange) interface, an integer range and a range of those integer ranges. DMD tells me the "transposed" template cannot deduce function from the arguments, which is a range of integer ranges. The template does, however, work on a two-dimensional array. The "save" method is implemented using a simple "return this", could this be to blame?
Re: turning an array of structs into a struct of arrays
Posted short write-up here. Please make it better... http://wiki.dlang.org/Transforming_slice_of_structs_into_struct_of_slices
Re: turning an array of structs into a struct of arrays
I can use FieldTypeTuple and FieldNameTuple, but I am a bit lost as to how without static foreach to loop through these in order to generate a mixin to declare the new type. I can turn it into a string, but what is the better option? The simplest solution is something like: template SOA(Struct, size_t LENGTH) { struct SOA { enum MEMBERNAME(size_t N) = __traits(identifier, Struct.tupleof[N]); static __gentypes() { string ret; foreach (I, TYPE; typeof(Struct.tupleof)) ret ~= "align(16) typeof(Struct.tupleof["~I.stringof~"])["~LENGTH.stringof~"] " ~ MEMBERNAME!I ~ ";"; return ret; } mixin(__gentypes()); } } alias PriceBars = SOA!(PriceBar, 8); which you'll have to adjust to your requirements (eg drop the '"~LENGTH.stringof~"' part to get a struct-of-slices, which is what your example above shows). artur Thanks to Artur, John Colvin, and Babu for the very helpful responses. This should be up on the wiki, and ideally someone should write a little tutorial text talking the reader through how this works and why choices were made a certain way. I will stick these up on the wiki with attribution if there are no objections. Something is better than perfect in this situation. There is a big gap between understanding what language terms do and knowing in practice how to use them for metaprogramming if you don't previously come from a C++ background. I originally learnt C with parameters in K&R style, and the world has changed quite a lot since those days - both the relative slowness of memory, and CTFE/metaprogramming are quite new concepts to me (Forth doesn't count). It's very easy to be quickly productive in D for most things (I found it easier than Python, which I learnt previously) but there is a bigger leap in learning templates+CTFE - not because it's intrinsically hard, but it's a different way of thinking that is unfamiliar. Probably I should read more source code - the harder way is often easier in the longer term. In case it's of interest my own reason for wanting to do this transformation was not cache efficiency, but just to be able to easily get the data into a form I can access from PydMagic and PyD, since for some chart applications it's easier to have the data in this form, and I would rather do all transformations in D and stick to Python only for pure charting. I appreciate the thoughtful responses. Laeeth.
Re: turning an array of structs into a struct of arrays
On 07/03/15 12:52, Laeeth Isharc via Digitalmars-d-learn wrote: > I have an array of structs eg > > struct PriceBar > { > DateTime date; > double open; > double high; > double low; > double close; > } > > (which fields are present in this particular struct will depend on template > arguments). > > what is the best way to turn these at compile time into a struct of arrays? eg > > struct PriceBars > { > DateTime[] dates; > double[] opens; > double[] highs; > double[] lows; > double[] closes; > } > > I can use FieldTypeTuple and FieldNameTuple, but I am a bit lost as to how > without static foreach to loop through these in order to generate a mixin to > declare the new type. I can turn it into a string, but what is the better > option? The simplest solution is something like: template SOA(Struct, size_t LENGTH) { struct SOA { enum MEMBERNAME(size_t N) = __traits(identifier, Struct.tupleof[N]); static __gentypes() { string ret; foreach (I, TYPE; typeof(Struct.tupleof)) ret ~= "align(16) typeof(Struct.tupleof["~I.stringof~"])["~LENGTH.stringof~"] " ~ MEMBERNAME!I ~ ";"; return ret; } mixin(__gentypes()); } } alias PriceBars = SOA!(PriceBar, 8); which you'll have to adjust to your requirements (eg drop the '"~LENGTH.stringof~"' part to get a struct-of-slices, which is what your example above shows). artur
Re: Switching rows with columns
On Saturday, 4 July 2015 at 15:28:56 UTC, Tanel Tagaväli wrote: I have a range of ranges and need to change it so the elements are column-aligned instead of row-aligned. For example, [[1, 2, 3], [4, 5, 6], [7, 8, 9]] would change into [[1, 4, 7], [2, 5, 8], [3, 6, 0]]. Can I even do this with ranges, and if so, how? Try std.range.transposed: http://dlang.org/phobos/std_range.html#.transposed
Switching rows with columns
I have a range of ranges and need to change it so the elements are column-aligned instead of row-aligned. For example, [[1, 2, 3], [4, 5, 6], [7, 8, 9]] would change into [[1, 4, 7], [2, 5, 8], [3, 6, 0]]. Can I even do this with ranges, and if so, how?
Re: Array operations, dynamic arrays and length
On Thursday, 2 July 2015 at 19:27:57 UTC, J Miller wrote: I knew that automatic allocation doesn't happen, but I'm confused by the fact if you explicitly declare "c" with "int[] c;" and then assign "c[] = a[] * b[]", versus using "auto c = a[] * b[]", you get two different errors (array length mismatch vs no destination memory). I find it confusing as well. For me it's about consistency of syntax. For instance, auto z = x+y works with numeric types, but the equivalent you use for arrays doesn't work. It just means one more thing to remember.
Re: How to setup GDC with Visual D?
On Saturday, 4 July 2015 at 08:34:00 UTC, Johannes Pfau wrote: It's kinda fascinating that GDC/MinGW seems to work for some real world applications. I haven't really tried a "real world application" as of yet; mostly small puzzle-type problems to get a feel for D. I did run into a problem with this code: int answer = to!(int[])(split("7946590 6020978")).sum; It compiles fine under DMD but gives the error "Error: no property 'sum' for type 'int[]'" with GDC. Then 10x larger binaries are indeed caused by debug info. If you don't need the debug info you can use the included strip.exe to remove it: strip.exe yourapp.exe Yes, that helps. My exe went from 6.9MB to 1MB. The DMD exe is 370K.
Re: How to setup GDC with Visual D?
Am Sat, 04 Jul 2015 06:30:31 + schrieb "Marko Grdinic" : > On Friday, 3 July 2015 at 23:45:15 UTC, Guy Gervais wrote: > > On Friday, 3 July 2015 at 19:17:28 UTC, Marko Grdinic wrote: > >> Any advice regarding how I can get this to work? Thanks. > > > > I got GDC to work with VS2013 + VisualD by going into > > "Tools->Options" (The VS menu, not the one under "Visual D") > > and adding the paths under "Projects and Solutions -> Visual D > > Settings -> GDC Directories". I put the path to the bin folder > > in MinGW64 and the bin folder in GDC. > > > > I get a 10%-15% speed improvement, which is nice, but my > > binaries are 10 times larger. > > I have no idea where Visual D is supposed to be looking at, but I > managed to get it to work by adding the Gdc/bin directory into > path. With that it finds it anywhere. It's kinda fascinating that GDC/MinGW seems to work for some real world applications. Please note that it's in early alpha state, mostly unsupported and not really well-tested though. (I hope this will change later this year) Then 10x larger binaries are indeed caused by debug info. If you don't need the debug info you can use the included strip.exe to remove it: strip.exe yourapp.exe