A little of coordination for Rosettacode

bearophile bearophileHUGS at lycos.com
Sun Mar 24 14:34:16 PDT 2013


Jos van Uden:

> I think it's convenient, it checks for null or empty, I don't 
> find it confusing at all.

I agree it's shorter and it looks handy. But one of its problems 
is that in D there are arrays with length zero that aren't null:


import std.stdio;
int[] foo() {
     auto a = [1];
     return a[0..0];
}
void main() {
     if (auto data = foo()) {
         writeln("here");
     }
}


In the vampire code this doesn't happen because you return null 
(that is represented by two zero words), but in general unlike 
Python in D the safe and idiomatic way to test for array 
emptiness is to use std.array.empty. Otherwise you risk having 
problems.


> With regards to immutablity: I do find it useful, but it 
> creates an anormous amount of
> code clutter. So I use it only where it's really important. For 
> instance we have f1 and
> f2, they are declared const, and then only two lines later the 
> constness has to be cast
> away again. I find that a bit over the top.

f1 and f2 can be declared immutable, because they don't need to 
change.

The later cast was a mistake of mine, I am sorry. In real-world D 
code in a situation like that I do as you have done, making f1 
and f2 mutable.

But on Rosettacode I think it's important to underline the 
current problems in the D language itself. In an hypothetical 
future version of D if you cancat two immutable arrays you should 
get something that is typed as immutable, but is implicitly 
castable to mutable :-)

I will think a bit more about this. Maybe I will just make f1 and 
f2 mutable, it's the cleanest solution.


>> - "if (digits.length % 2)" instead of "if (digits.length & 
>> 1)": for the compiler they are exactly the same, because
>> the value is unsigned. And using a modulus is more clear here. 
>> We are discussing about parity, not about bits.
>
> That is a matter of opinion. If I see "% 2", my first thought 
> is: we're checking for even... then I realize the
> "== 0" is missing.

I will add the == 0 too then.


> I wrote the task myself, so I should know.

Ah :-) I didn't know. OK. So it's the Python entry to be wrong 
and yours is correct.


> It's fine with me. I'm glad we got rid of the ugly cast.

I agree, that cast in D was a bug of mine. (I try to fix mistakes 
in your code, but once in a while my code introduces other 
bugs/problems. In such cases on GitHub you can annotate code, but 
on mediawiki you have to just "fix" the code. It wasn't designed 
for code.)

Later I will change the code on Rosettacode.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list