Wiki

Changeset 1769

Show
Ignore:
Timestamp:
11/16/08 21:02:05 (4 years ago)
Author:
Chuck.Esterbrook
Message:

Regarding "truthfulness" such as found in assert, if and if(...), primitive nilable types such as bool? and int? now have their value examined. Previously, simply being non-nil was enough to be true, but that was not intuitive nor consistent with dynamic?.
ticket:5

Location:
cobra/trunk
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Developer/IntermediateReleaseNotes.text

    r1767 r1769  
    128128* The default type for vars, properties and method arguments is now `dynamic?`. Also the result type for `d.foo` where `d` is dynamic is now `dynamic?` instead of `dynamic`. These changes reflect the flexibility that was originally intended with default types and dynamic typing. 
    129129 
     130* Regarding "truthfulness" such as found in `assert`, `if` and `if(...)`, primitive nilable types such as `bool?` and `int?` now have their value examined. Previously, simply being non-nil was enough to be true, but that was not intuitive nor consistent with `dynamic?`. ticket:5 
     131 
    130132* Added new `all` and `any` unary operators that take something enumerable (such as a list, set or generator) and return a boolean (true or false) indicating if all or any of the elements are true. These operators increase expressiveness in conditions. For example, a contract might use these operators: 
    131133.code 
  • cobra/trunk/Source/CobraLang.cs

    r1643 r1769  
    622622    } 
    623623 
     624    static public bool IsTrue(char? c) { 
     625        return c!=null && c.Value!='\0'; 
     626    } 
     627 
    624628    static public bool IsTrue(int i) { 
    625629        return i!=0; 
    626630    } 
    627631 
     632    static public bool IsTrue(int? i) { 
     633        return i!=null && i.Value!=0; 
     634    } 
     635 
    628636    static public bool IsTrue(decimal d) { 
    629637        return d!=0; 
    630638    } 
    631639 
     640    static public bool IsTrue(decimal? d) { 
     641        return d!=null && d.Value!=0; 
     642    } 
     643 
    632644    static public bool IsTrue(float f) { 
    633645        return f!=0; 
    634646    } 
    635647 
     648    static public bool IsTrue(float? f) { 
     649        return f!=null && f.Value!=0; 
     650    } 
     651 
    636652    static public bool IsTrue(double d) { 
    637653        return d!=0; 
     654    } 
     655 
     656    static public bool IsTrue(double? d) { 
     657        return d!=null && d.Value!=0; 
    638658    } 
    639659 
     
    647667        return c!=null; 
    648668    } 
     669 
     670/* 
     671    static public bool IsTrue<T>(Nullable<T> x) where T : struct { 
     672        if (x == null) 
     673            return false; 
     674        else 
     675            return IsTrue(x.Value);  // would be nice if this respected overloads, but it doesn't. always calls IsTrue(object) which makes this method moot 
     676    } 
     677*/ 
    649678 
    650679    static public bool IsTrue(object x) { 
     
    659688        if (x is decimal) 
    660689            return (decimal)x!=0;  // can't believe x.Equals(0) above doesn't work for decimal. *sigh* 
     690        if (x is double) 
     691            return (double)x!=0; 
     692        if (x is float) 
     693            return (float)x!=0; 
    661694        return true; 
    662695    } 
  • cobra/trunk/Source/Expr.cobra

    r1768 r1769  
    19331933            _expr.bindImp 
    19341934        else if type inherits NilableType 
    1935             if type.nonNil inherits DynamicType 
     1935            if type.nonNil inherits DynamicType or type.nonNil inherits PrimitiveType 
    19361936                _treatment = Treatment.InvokeRuntime 
    19371937            else 
  • cobra/trunk/Tests/240-generics/200-nilables/102-nilable-int.cobra

    r1177 r1769  
    1 # can assign int to int? 
    21class Test 
    32 
    43    shared 
    54        def main 
     5            # can assign int to int? 
    66            i as int? 
    77            j as int 
     
    99            i = j 
    1010            assert i==0 
     11 
     12