Changes between Version 12 and Version 13 of MixIn
- Timestamp:
- 07/29/13 11:50:07 (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
MixIn
v12 v13 3 3 == Introduction == 4 4 5 To be done. In the mean time, see the [http://cobra-language.com/forums/viewtopic.php?f=4&t=306 discussion thread]. 5 Provide a non inheritance based capability for shared/common behaviour. 6 7 Basically, selected traits or behaviours are described in a special Type called a 'mixin' (analogous to an interface),[[BR]] 8 these can then be specified on a class and are injected into that class to provide those behaviours on that class. 9 10 11 Mixin behaviours can be specified using object: 12 * vars 13 * properties 14 * methods 15 They do not support constructors 16 17 (For more see the [http://cobra-language.com/forums/viewtopic.php?f=4&t=306 Forum discussion thread]). 6 18 7 19 == Compared to Extensions == … … 14 26 * Mixins cannot be used from a [wiki:BinaryLibrary binary library]. The source must be included for the compiler. 15 27 16 In summary, mixins are more about multiple inheritance among the types you are declaring. Extensions are about adding convenience methods to existing types (regardless of who declared them). 28 In summary, mixins are more about implementing shared capabilities among the types you are declaring. [[BR]] 29 Extensions are about adding convenience methods to existing types (regardless of who declared them). 17 30 18 31 == Restrictions == … … 28 41 * Two or more mixins added to a single type cannot have collision in their member names. 29 42 43 == Example == 44 {{{ 45 #!cobra 46 47 # The behaviour desired - a name property 48 mixin Named 49 var _name as String? 50 pro name as String 51 get 52 return _name ? '' 53 set 54 _name = value 55 56 get capName 57 return _name[:1].toUpper + _name[1:] 58 59 60 # the (otherwise empty) class with mixin added 61 class A adds Named 62 pass 63 64 def main is shared 65 a = A() 66 assert a inherits Named 67 assert a.name == '' 68 a.name = 'arosebyanyothername' 69 assert a.name == 'arosebyanyothername' 70 assert a.capName == 'Arosebyanyothername' 71 }}} 72 30 73 == See also == 31 74