Changeset 1563
- Timestamp:
- 05/27/10 20:43:04 (15 years ago)
- Files:
-
- trunk/phobos/std/typecons.d (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/typecons.d
r1561 r1563 1412 1412 1413 1413 +/ 1414 1414 1415 1415 1416 1416 /** 1417 1417 $(D BlackHole!Base) is a subclass of $(D Base) which automatically implements 1418 1418 all abstract member functions in $(D Base) as do-nothing functions. Each 1419 1419 auto-implemented function just returns the default value of the return type 1420 1420 without doing anything. 1421 1421 1422 The name came from 1423 $(WEB search.cpan.org/~sburke/Class-_BlackHole-0.04/lib/Class/_BlackHole.pm, Class::_BlackHole) 1424 Perl module by Sean M. Burke. 1425 1422 1426 Example: 1423 1427 -------------------- 1424 1428 abstract class C 1425 1429 { 1426 1430 int m_value; 1427 1431 this(int v) { m_value = v; } 1428 1432 int value() @property { return m_value; } 1429 1433 1430 1434 abstract real realValue() @property; 1431 1435 abstract void doSomething(); … … 1476 1480 1477 1481 assert(c.realValue !<>= 0); // NaN 1478 1482 c.doSomething(); 1479 1483 } 1480 1484 } 1481 1485 1482 1486 1483 1487 /** 1484 1488 $(D WhiteHole!Base) is a subclass of $(D Base) which automatically implements 1485 1489 all 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. 1490 function fails with throwing an $(D Error) and does never return. Useful for 1491 trapping use of not-yet-implemented functions. 1492 1493 The name came from 1494 $(WEB search.cpan.org/~mschwern/Class-_WhiteHole-0.04/lib/Class/_WhiteHole.pm, Class::_WhiteHole) 1495 Perl module by Michael G Schwern. 1488 1496 1489 1497 Example: 1490 1498 -------------------- 1491 1499 class C 1492 1500 { 1493 1501 abstract void notYetImplemented(); 1494 1502 } 1495 1503 1496 1504 void main() 1497 1505 { 1498 1506 auto c = new WhiteHole!C; 1499 c.notYetImplemented(); // throws AssertError1507 c.notYetImplemented(); // throws an Error 1500 1508 } 1501 1509 -------------------- 1510 1511 BUGS: 1512 Nothrow functions cause program to abort in release mode because the trap is 1513 implemented with $(D assert(0)) for nothrow functions. 1502 1514 1503 1515 See_Also: 1504 1516 AutoImplement, generateAssertTrap 1505 1517 */ 1506 1518 template WhiteHole(Base) 1507 1519 { 1508 1520 alias AutoImplement!(Base, generateAssertTrap, isAbstractFunction) 1509 1521 WhiteHole; 1510 1522 } 1511 1523 1524 // / ditto 1525 class NotImplementedError : Error 1526 { 1527 this(string method) 1528 { 1529 super(method ~ " is not implemented"); 1530 } 1531 } 1532 1512 1533 unittest 1513 1534 { 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 } 1514 1550 // doc example 1515 1551 { 1516 1552 static class C 1517 1553 { 1518 1554 abstract void notYetImplemented(); 1519 1555 } 1520 1556 1521 1557 auto c = new WhiteHole!C; 1522 1558 try 1523 1559 { … … 2227 2263 }; 2228 2264 else 2229 2265 enum string generateEmptyFunction = q{ 2230 2266 return typeof(return).init; 2231 2267 }; 2232 2268 } 2233 2269 2234 2270 /// ditto 2235 2271 template generateAssertTrap(C, func.../+[BUG 4217]+/) 2236 2272 { 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 }
