leonardo

View: Recent Entries. View: Archive. View: Friends. View: Profile. View: Website (My Website). August 11th, 2010 Tags: d language, gotcha, python Security: Subject: Some D gotchas for Python programmers Time: 08:06 pm



http://www.fantascienza.net/leonardo/ar/d_gotchas_for_python.html Update Dec 7 2010: article moved to the site and updated: comments: Leave a comment

(Anonymous) Link: (Link) Time: 2010-08-26 08:18 pm (UTC) Thanks

Very useful information for novice in D (Reply) (Thread)



leonardo_m Link: (Link) Time: 2010-08-26 09:19 pm (UTC) Yay, one reader! :-)

You are welcome. (Reply) (Parent) (Thread)



leonardo_m Link: (Link) Time: 2010-09-03 12:10 pm (UTC) annotype may be useful when you need to to translate Python code to D, it shows you the argument types:

http://www.partiallydisassembled.net/blog/?item=166 (Reply) (Thread)

ext_289694 Subject: Interesting Link: (Link) Time: 2010-10-18 12:22 pm (UTC) Another reader here :)



Interesting, thanks. My current playground project for learning D involves a lot of Python translation, so this article is very useful. I avoid most of these problems because I'm of the school of "explicit is better, even if its verbose" (ref when you want to change something inside a function, not assuming boolean results for comparisons, etc).



But yesterday I was already bitten by indexOf returning -1 and assigning the value to an uint. (Reply) (Thread)

(Anonymous) Subject: Great post Link: (Link) Time: 2010-10-19 07:11 pm (UTC) I havent started using D yet, as I havent needed to step out of Python for a while, but it's where I plan to go when I do, so thanks! :) (Reply) (Thread)

ext_292431 Subject: Great Article. Link: (Link) Time: 2010-10-20 04:13 pm (UTC) I enjoyed your summary. I am a D2 novice (just finishing reading The D Programming Language), but am enjoying learning it immensely.



Some of your examples are lacking "answers", however. You very nicely point out the issues, but do not always propose solutions/workarounds, which I would appreciate.



For example, how (if possible) can you bind a list/dict element to a reference in the current scope, in order to modify it in place?

When I tried to use _ref_ I got "only parameters or foreach declarations can be ref", and settled on using pointers (which, as a C++ guy, I hate having to resort to for such simple cases). The last option I found was using a delegate (which is obviously not code I would write, but I add for completeness) :





(ref int[] d1 = d[1]) {

d1 ~= 5;

}();





The first line is syntactically similar to just declaring a reference variable, the following block has access to all stack variables, and the final line "calls" the delegate. Other than having about 5 extra characters, an extra line, and being a hack, the issue with such code is that you cannot use "auto" to deduce the type of d[1], and I haven't found a way to write parameterized delegates (not sure if its possible).



Most of your other examples haven't really "bitten" me yet, but I share your distaste for list access using "$" compared to python. It may indeed be for performance reasons, but I don't see why there could not be an implied $ at least for compile time constants: [-1]->[$-1], and [..]->[0..$]. Finally, I dont like the ".." syntax as much as ":", though that may just take some adjusting (".." is certainly more natural).



Thanks for the read! (Reply) (Thread)



leonardo_m Subject: Re: Great Article. Link: (Link) Time: 2010-10-20 04:42 pm (UTC) > Some of your examples are lacking "answers", however.



I am willing to improve the article, where possible.





> For example, how (if possible) can you bind a list/dict element to a reference in the current scope, in order to modify it in place?



I don't understand what you are trying to do.



Among D built-ins there are dynamic and fixed-sized arrays, and associative arrays (dicts).



If it's an object, it's managed by reference. In D there is no way to build a generic reference in the current scope, you have to use pointers. But in D it's better to use pointers only where there's no other solution.



In Python dict keys need to be immutable, likewise if you modify a key in place in D, its hash function may change, so this may cause troubles.





> the issue with such code is that you cannot use "auto" to deduce the type of d[1], and I haven't found a way to write parameterized delegates (not sure if its possible).



If you mean templates, it's possible to write a template delegate in place.





> but I share your distaste for list access using "$" compared to python.



The usage of $ in D is a very good solution that keeps the performance of normal arrays while giving some of the semantic flexibility of Python lists. For a high-performance language as D that may be the best solution.





> but I don't see why there could not be an implied $ at least for compile time constants: [-1]->[$-1], and [..]->[0..$].



For consistency. Special cases kill languages (See C++).





> Finally, I dont like the ".." syntax as much as ":", though that may just take some adjusting (".." is certainly more natural).



It's just a small syntax difference. And the semicolon inside a [] is a syntax already taken for two other purposes (array literals and associative array literals). (Reply) (Parent) (Thread)

leonardo

View: Recent Entries. View: Archive. View: Friends. View: Profile. View: Website (My Website).