Changes between Version 4 and Version 5 of TypeInference
- Timestamp:
- 11/22/10 20:27:46 (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TypeInference
v4 v5 3 3 Although you can explicitly declare the type of a local variable, you don't have to: 4 4 {{{ 5 #!cobra 5 6 # the long way: 6 7 i as int = 0 … … 13 14 This also works for object variables: 14 15 {{{ 16 #!cobra 15 17 class Person 16 18 … … 21 23 If the inferred type is not suitable, perhaps because you need a more general type, you can provide one explicitly or even typecast the right-hand side: 22 24 {{{ 25 #!cobra 23 26 class Person 24 27 … … 33 36 Sometimes you want the variable to be typed as a base class because you may assign other things to it in the future: 34 37 {{{ 38 #!cobra 35 39 shape = Circle() to Shape 36 40 … … 42 46 Type inference also occurs for the types of generic lists, sets and dictionaries: 43 47 {{{ 48 #!cobra 44 49 names = ['foo', 'bar'] # List<of String> 45 50 numbers = {1, 2, 3} # Set<of int>