Changeset 1897
- Timestamp:
- 08/19/10 00:48:56 (14 years ago)
- Files:
-
- trunk/docsrc/changelog.dd (modified) (1 diff)
- trunk/phobos/std/traits.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/docsrc/changelog.dd
r1896 r1897 4 4 5 5 $(VERSION 049, Aug 11, 2010, =================================================, 6 6 7 7 8 8 $(WHATSNEW 9 9 $(LI Added FFT to std.numeric.) 10 10 $(LI Added convenience functions for Rebindable.) 11 11 $(LI std.process: Added environment, an AA-like interface for environment variables.) 12 12 $(LI std.range: Iota, Stride, Transversal, FrontTransveral now support slicing where possible.) 13 13 $(LI std.range: Added Lockstep, hasLvalueElements.) 14 $(L1 std.traits: Added isIterable, ForeachType.) 14 15 $(LI std.typetuple: Added anySatisfy.) 15 16 ) 16 17 $(BUGSFIXED 17 18 $(LI Unlisted Bug: std.math.pow doesn't work on immutable numbers.) 18 19 $(LI Unlisted Bug: std.math.pow floating point overload expects both arguments to be exact same type.) 19 20 $(LI Unlisted Bug: std.path.join("", "foo") returns "/foo" instead of "foo" on Posix.) 20 21 $(LI Unlisted Bug: std.range.iota() random access primitives inconsistent after popFront on floating point version) 21 22 $(LI Several unlisted bugs in std.range.chain) 22 23 $(LI $(BUGZILLA 2903): Splitter should be bi-dir if the input range is bi-dir.) 23 24 $(LI $(BUGZILLA 2951): std.random.dice() should be templated on proportions.) trunk/phobos/std/traits.d
r1870 r1897 2310 2310 unittest 2311 2311 { 2312 2312 static assert(isPointer!(int*)); 2313 2313 static assert(!isPointer!(uint)); 2314 2314 static assert(!isPointer!(uint[uint])); 2315 2315 static assert(!isPointer!(char[])); 2316 2316 static assert(isPointer!(void*)); 2317 2317 } 2318 2318 2319 2319 /** 2320 * Returns $(D true) if T can be iterated over using a $(D foreach) loop with 2321 * a single loop variable of automatically inferred type, regardless of how 2322 * the $(D foreach) loop is implemented. This includes ranges, structs/classes 2323 * that define $(D opApply) with a single loop variable, and builtin dynamic, 2324 * static and associative arrays. 2325 */ 2326 template isIterable(T) 2327 { 2328 static if (is(typeof({foreach(elem; T.init) {}}))) { 2329 enum bool isIterable = true; 2330 } else { 2331 enum bool isIterable = false; 2332 } 2333 } 2334 2335 unittest { 2336 struct OpApply 2337 { 2338 int opApply(int delegate(ref uint) dg) { assert(0); } 2339 } 2340 2341 struct Range 2342 { 2343 uint front() { assert(0); } 2344 void popFront() { assert(0); } 2345 enum bool empty = false; 2346 } 2347 2348 static assert(isIterable!(uint[])); 2349 static assert(!isIterable!(uint)); 2350 static assert(isIterable!(OpApply)); 2351 static assert(isIterable!(uint[string])); 2352 static assert(isIterable!(Range)); 2353 } 2354 2355 /** 2320 2356 * Tells whether the tuple T is an expression tuple. 2321 2357 */ 2322 2358 template isExpressionTuple(T ...) 2323 2359 { 2324 2360 static if (T.length > 0) 2325 2361 enum bool isExpressionTuple = 2326 2362 !is(T[0]) && __traits(compiles, { auto ex = T[0]; }) && 2327 2363 isExpressionTuple!(T[1 .. $]); 2328 2364 else 2329 2365 enum bool isExpressionTuple = true; // default … … 2585 2621 } 2586 2622 2587 2623 unittest 2588 2624 { 2589 2625 static assert(is(ModifyTypePreservingSTC!(Intify, const real) == const int)); 2590 2626 static assert(is(ModifyTypePreservingSTC!(Intify, immutable real) == immutable int)); 2591 2627 static assert(is(ModifyTypePreservingSTC!(Intify, shared real) == shared int)); 2592 2628 static assert(is(ModifyTypePreservingSTC!(Intify, shared(const real)) == shared(const int))); 2593 2629 } 2594 2630 version (unittest) private template Intify(T) { alias int Intify; } 2631 2632 /** 2633 Returns the inferred type of the loop variable when a variable of type T 2634 is iterated over using a $(D foreach) loop with a single loop variable and 2635 automatically inferred return type. Note that this may not be the same as 2636 $(D std.range.ElementType!(Range)) in the case of narrow strings, or if T 2637 has both opApply and a range interface. 2638 */ 2639 template ForeachType(T) 2640 { 2641 alias ReturnType!(typeof( 2642 { 2643 foreach(elem; T.init) { 2644 return elem; 2645 } 2646 assert(0); 2647 })) ForeachType; 2648 } 2649 2650 unittest 2651 { 2652 static assert(is(ForeachType!(uint[]) == uint)); 2653 static assert(is(ForeachType!(string) == immutable(char))); 2654 static assert(is(ForeachType!(string[string]) == string)); 2655 } 2595 2656 2596 2657 2597 2658 /** 2598 2659 Strips off all $(D typedef)s (including $(D enum) ones) from type $(D T). 2599 2660 2600 2661 Example: 2601 2662 -------------------- 2602 2663 enum E : int { a } 2603 2664 typedef E F; 2604 2665 typedef const F G;
