Wiki

Changes between Version 2 and Version 3 of TypeExtensions

Show
Ignore:
Timestamp:
07/29/13 12:13:27 (4 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TypeExtensions

    v2 v3  
    11== Type Extensions == 
    22 
    3 Woops. Not documented yet. Real quick: 
     3Extend an existing type with additional method(s). 
     4 
     5  
     6== Grammar == 
     7{{{ 
     8extend EXISTINGTYPE 
     9   def ADDMETHOD 
     10       ... 
     11   .... 
     12}}} 
     13 
     14== Example == 
     15 
    416{{{ 
    517#!cobra 
     
    820    def doubleLength as int 
    921        return .length * 2 
     22 
     23    def capitalized as String 
     24        """ 
     25        Returns the string with the first character capitalized. 
     26        Returns a blank string for a blank string. 
     27        """ 
     28        ensure 
     29                result.length == .length 
     30                result.length implies result[0] == result[0].toUpper 
     31        test 
     32                assert 'chuck'.capitalized == 'Chuck' 
     33                assert 'Chuck'.capitalized == 'Chuck' 
     34                assert ''.capitalized == '' 
     35                assert ' foo'.capitalized == ' foo' 
     36                assert 'f'.capitalized == 'F' 
     37                assert '1aoeu'.capitalized == '1aoeu' 
     38        body 
     39                if .length == 0, return this 
     40                return this[:1].toUpper + this[1:] 
    1041}}} 
    1142 
     43== Restrictions == 
     44You can use extension methods to extend a class or interface, but cannot override existing matching methods. 
     45 
     46 
    1247See also: StandardLibraryExtensionMethods, LanguageTopics