Changes between Version 2 and Version 3 of TypeExtensions
- Timestamp:
- 07/29/13 12:13:27 (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TypeExtensions
v2 v3 1 1 == Type Extensions == 2 2 3 Woops. Not documented yet. Real quick: 3 Extend an existing type with additional method(s). 4 5 6 == Grammar == 7 {{{ 8 extend EXISTINGTYPE 9 def ADDMETHOD 10 ... 11 .... 12 }}} 13 14 == Example == 15 4 16 {{{ 5 17 #!cobra … … 8 20 def doubleLength as int 9 21 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:] 10 41 }}} 11 42 43 == Restrictions == 44 You can use extension methods to extend a class or interface, but cannot override existing matching methods. 45 46 12 47 See also: StandardLibraryExtensionMethods, LanguageTopics