Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1563

Show
Ignore:
Timestamp:
05/27/10 20:43:04 (15 years ago)
Author:
rsinfu
Message:

typecons doc: made reference to the Class::BlackHole? and Class::WhiteHole? perl modules.
Plus made WhiteHole? to throw NotImplementedError? if possible.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/typecons.d

    r1561 r1563  
    14121412 
    14131413+/ 
    14141414 
    14151415 
    14161416/** 
    14171417$(D BlackHole!Base) is a subclass of $(D Base) which automatically implements 
    14181418all abstract member functions in $(D Base) as do-nothing functions.  Each 
    14191419auto-implemented function just returns the default value of the return type 
    14201420without doing anything. 
    14211421 
     1422The name came from 
     1423$(WEB search.cpan.org/~sburke/Class-_BlackHole-0.04/lib/Class/_BlackHole.pm, Class::_BlackHole) 
     1424Perl module by Sean M. Burke. 
     1425 
    14221426Example: 
    14231427-------------------- 
    14241428abstract class C 
    14251429{ 
    14261430    int m_value; 
    14271431    this(int v) { m_value = v; } 
    14281432    int value() @property { return m_value; } 
    14291433 
    14301434    abstract real realValue() @property; 
    14311435    abstract void doSomething(); 
     
    14761480 
    14771481        assert(c.realValue !<>= 0); // NaN 
    14781482        c.doSomething(); 
    14791483    } 
    14801484} 
    14811485 
    14821486 
    14831487/** 
    14841488$(D WhiteHole!Base) is a subclass of $(D Base) which automatically implements 
    14851489all abstract member functions as throw-always functions.  Each auto-implemented 
    1486 function fails with $(D AsertError) and does never return.  Useful for trapping 
    1487 use of not-yet-implemented functions. 
     1490function fails with throwing an $(D Error) and does never return.  Useful for 
     1491trapping use of not-yet-implemented functions. 
     1492 
     1493The name came from 
     1494$(WEB search.cpan.org/~mschwern/Class-_WhiteHole-0.04/lib/Class/_WhiteHole.pm, Class::_WhiteHole) 
     1495Perl module by Michael G Schwern. 
    14881496 
    14891497Example: 
    14901498-------------------- 
    14911499class C 
    14921500{ 
    14931501    abstract void notYetImplemented(); 
    14941502} 
    14951503 
    14961504void main() 
    14971505{ 
    14981506    auto c = new WhiteHole!C; 
    1499     c.notYetImplemented(); // throws AssertError 
     1507    c.notYetImplemented(); // throws an Error 
    15001508} 
    15011509-------------------- 
     1510 
     1511BUGS: 
     1512  Nothrow functions cause program to abort in release mode because the trap is 
     1513  implemented with $(D assert(0)) for nothrow functions. 
    15021514 
    15031515See_Also: 
    15041516  AutoImplement, generateAssertTrap 
    15051517 */ 
    15061518template WhiteHole(Base) 
    15071519{ 
    15081520    alias AutoImplement!(Base, generateAssertTrap, isAbstractFunction) 
    15091521            WhiteHole; 
    15101522} 
    15111523 
     1524// / ditto 
     1525class NotImplementedError : Error 
     1526{ 
     1527    this(string method) 
     1528    { 
     1529        super(method ~ " is not implemented"); 
     1530    } 
     1531} 
     1532 
    15121533unittest 
    15131534{ 
     1535    // nothrow 
     1536    debug // see the BUGS above 
     1537    { 
     1538        interface I_1 
     1539        { 
     1540            void foo(); 
     1541            void bar() nothrow; 
     1542        } 
     1543        auto o = new WhiteHole!I_1; 
     1544        uint trap; 
     1545        try { o.foo(); } catch (Error e) { ++trap; } 
     1546        assert(trap == 1); 
     1547        try { o.bar(); } catch (Error e) { ++trap; } 
     1548        assert(trap == 2); 
     1549    } 
    15141550    // doc example 
    15151551    { 
    15161552        static class C 
    15171553        { 
    15181554            abstract void notYetImplemented(); 
    15191555        } 
    15201556 
    15211557        auto c = new WhiteHole!C; 
    15221558        try 
    15231559        { 
     
    22272263        }; 
    22282264    else 
    22292265        enum string generateEmptyFunction = q{ 
    22302266            return typeof(return).init; 
    22312267        }; 
    22322268} 
    22332269 
    22342270/// ditto 
    22352271template generateAssertTrap(C, func.../+[BUG 4217]+/) 
    22362272{ 
    2237     enum string generateAssertTrap = 
    2238         `assert(0, "` ~ (C.stringof ~ "." ~ __traits(identifier, func)) 
    2239                 ~ ` is not implemented");`; 
    2240 
     2273    static if (functionAttributes!(func) & FunctionAttribute.NOTHROW) //XXX 
     2274    { 
     2275        pragma(msg, "Warning: WhiteHole!(", C, ") used assert(0) instead " 
     2276                "of Error for the auto-implemented nothrow function ", 
     2277                C, ".", __traits(identifier, func)); 
     2278        enum string generateAssertTrap = 
     2279            `assert(0, "` ~ C.stringof ~ "." ~ __traits(identifier, func) 
     2280                    ~ ` is not implemented");`; 
     2281    } 
     2282    else 
     2283        enum string generateAssertTrap = 
     2284            `throw new NotImplementedError("` ~ C.stringof ~ "." 
     2285                    ~ __traits(identifier, func) ~ `");`; 
     2286