Spec#, nullables and more

Walter Bright newshound2 at digitalmars.com
Sat Nov 6 18:42:02 PDT 2010


Roman Ivanov wrote:
> But it's not a specific case if you look at it from OOP perspective!
> 
> The problem of restricting types was solved in OOP. Every time you
> inherit a class, you make it more specific. This allows you to restrict
> types all you want, and the compiler enforces your restrictions.
> 
> For example, you can create a class that represents an even number and
> accepts only even numbers in its constructor (trowing exception
> otherwise). You can then extend it and create a class that only accepts
> every second even number, and so on. Since D is a statically typed
> language, attempts to assign arbitrary class to a variable intended for
> Even objects is caught at compile time (instead of waiting until someone
> references a missing method or attribute at run-time).
> 
> Null is the only wrench in the gears here.


Your insight that this is related to OOP is right on target. It's polymorphism, 
but a different kind of polymorphism than class OOP tries to solve.


More information about the Digitalmars-d mailing list