Does D have too many features?
H. S. Teoh
hsteoh at quickfur.ath.cx
Sat Apr 28 15:17:42 PDT 2012
On Sat, Apr 28, 2012 at 11:12:38PM +0200, SomeDude wrote:
> On Saturday, 28 April 2012 at 20:59:48 UTC, q66 wrote:
> >
> >This kind of attitude "we need big fat bullshit like Java and
> >heavy use of OO and idioms and EH and all that other crap" is
> >broken and false. And you have no way to prove that Python for
> >example wouldn't scale for large projects; its main fault is that
> >the default implementation is rather slow, but it's not pretty
> >much missing anything required for a large project.
>
> Python has two big drawbacks for large projects:
> - it's too slow
> - it's a dynamically-typed language
>
> The fact that it's flexible is because it uses duck typing, and
> AFAIK you can't do duck typing in a statically typed language.
> So it's cool for small programs, but it can't handle large ones
> because it's not statically typed. And this opinion doesn't come
> just out of thin air, I speak from my own professional experience.
Who says D doesn't have duck-typing?
template isADuck(T) {
alias isADuck = is(typeof(T.quack())==bool);
}
void petADuck(T)(T duck)
if (isADuck!T)
{
duck.quack();
}
You can pass anything that has a quack() method to the function:
struct CanadianDuck {
void quack() { writeln("Quack, eh?"); }
}
class AmericanDuck {
void quack() { writeln("Quack, yo!"); }
}
struct RubberDuck {
void opDispatch!(string S)()
{
auto d = loadRuntimeDuckClass();
d.callMethod(S);
}
}
struct Cow {
void moo() { writeln("Mooo!"); }
}
void main() {
CanadianDuck caddie;
AmericanDuck quacker = new AmericanDuck;
RubberDuck runtimeDuck;
Cow orker;
// Look, ma! I hez duck-taiping!
petADuck(caddie);
petADuck(quacker);
petADuck(runtimeDuck);
// Reject non-duck objects
petADuck(orker); // compile error: orker is not a duck!
}
Not only D supports duck-typing, the compiler even checks type-safety
for you at compile-time. ;-)
Incidentally, this is what the Phobos range interface does.
T
--
My program has no bugs! Only undocumented features...
More information about the Digitalmars-d
mailing list