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

Changeset 491

Show
Ignore:
Timestamp:
01/09/11 06:39:21 (14 years ago)
Author:
andrei
Message:

.dup should only work for mutable associative arrays

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/import/object.di

    r488 r491  
    402402    { 
    403403        return &opApply; 
    404404    } 
    405405 
    406406    Value get(Key key, lazy Value defaultValue) 
    407407    { 
    408408        auto r = key in *cast(Value[Key]*)(&p); 
    409409        return r ? *r : defaultValue; 
    410410    } 
    411411 
    412     @property Value[Key] dup() 
    413     { 
    414         Value[Key] result; 
    415         foreach (k, v; this) 
     412    static if (is(typeof({ Value[Key] r; r[Key.init] = Value.init; }()))) 
     413        @property Value[Key] dup() 
    416414        { 
    417             result[k] = v; 
     415            Value[Key] result; 
     416            foreach (k, v; this) 
     417            { 
     418                result[k] = v; 
     419            } 
     420            return result; 
    418421        } 
    419         return result; 
    420     } 
    421422} 
    422423 
    423424unittest 
    424425{ 
    425426    auto a = [ 1:"one", 2:"two", 3:"three" ]; 
    426427    auto b = a.dup; 
    427428    assert(b == [ 1:"one", 2:"two", 3:"three" ]); 
    428429} 
    429430 
    430431void clear(T)(T obj) if (is(T == class)) 
  • trunk/src/object_.d

    r488 r491  
    24482448    { 
    24492449        return &opApply; 
    24502450    } 
    24512451 
    24522452    Value get(Key key, lazy Value defaultValue) 
    24532453    { 
    24542454        auto p = key in *cast(Value[Key]*)(&p); 
    24552455        return p ? *p : defaultValue; 
    24562456    } 
    24572457 
    2458     @property Value[Key] dup() 
    2459     { 
    2460         Value[Key] result; 
    2461         foreach (k, v; this) 
    2462         { 
    2463             result[k] = v; 
    2464         } 
    2465         return result; 
    2466     } 
     2458    static if (is(typeof({ Value[Key] r; r[Key.init] = Value.init; }()))) 
     2459        @property Value[Key] dup() 
     2460        { 
     2461            Value[Key] result; 
     2462            foreach (k, v; this) 
     2463            { 
     2464                result[k] = v; 
     2465            } 
     2466            return result; 
     2467        } 
    24672468} 
    24682469 
    24692470unittest 
    24702471{ 
    24712472    auto a = [ 1:"one", 2:"two", 3:"three" ]; 
    24722473    auto b = a.dup; 
    24732474    assert(b == [ 1:"one", 2:"two", 3:"three" ]); 
    24742475} 
    24752476 
    24762477void clear(T)(T obj) if (is(T == class))