Ticket #230: ticket-230-c.patch
File ticket-230-c.patch, 40.3 KB (added by hopscc, 11 years ago) |
---|
-
Source/Expr.cobra
3348 3348 get isObjectLiteral as bool is override 3349 3349 return true 3350 3350 3351 get isTentativelyTyped as bool 3352 return false 3351 3353 3354 get typeInferred from var = false 3355 3356 3352 3357 class SequenceLit 3353 3358 is abstract, partial 3354 3359 inherits CompositeLiteral 3355 3360 3356 3361 var _exprs as List<of Expr> 3362 var _typeNode as ITypeProxy? # to-do: rename to _typeProxy 3357 3363 3358 cue init(token as IToken, exprs as List<of Expr> )3364 cue init(token as IToken, exprs as List<of Expr>, typeNode as ITypeProxy? = nil) 3359 3365 base.init(token) 3360 _exprs = exprs3366 _exprs, _typeNode = exprs, typeNode 3361 3367 3362 3368 def addSubFields is override 3363 3369 base.addSubFields … … 3377 3383 get willChangeVar as bool is override 3378 3384 for expr in _exprs, if expr.willChangeVar, return true 3379 3385 return false 3380 3386 3387 get isTentativelyTyped as bool is override 3388 # empty sequence and type not explicitly set 3389 assert .didBindImp 3390 return _exprs.count == 0 and _typeInferred == true 3391 3381 3392 def _bindImp is override 3382 3393 base._bindImp 3383 3394 exceptions = List<of NodeException>() … … 3388 3399 exceptions.add(ne) 3389 3400 if exceptions.count 3390 3401 throw NodeMultiException(exceptions) 3402 if _typeNode # optional explicit type 3403 _type = _convertTypeNode #_typeNode.realType 3404 assert _type, _typeNode 3405 seqType = _genExprsType 3391 3406 if _type is nil 3392 exprs = _exprs 3393 if exprs.count == 0 3394 type = .compiler.defaultType 3395 else 3396 type = exprs[0].type to ! 3397 for i in 1 : exprs.count 3398 exprs[i].bindImp 3399 type = exprs[i].type.greatestCommonDenominatorWith(type) 3407 _type = seqType 3408 _typeInferred = true 3409 else if _exprs.count > 0 # type set from [...] as List<of TYPE> 3410 if not seqType.isAssignableTo(_type to !) 3411 .throwError('The common type of the sequence of expressions is [seqType.name] which is not assignable to the explicitly declared type [_type.name].') 3412 3413 def _genExprsType as IType 3414 exprs = _exprs 3415 if exprs.count == 0 3416 type = .compiler.defaultType 3417 else 3418 type = exprs[0].type to ! 3419 for i in 1 : exprs.count 3420 #exprs[i].bindImp # is this necessary - done earlier in bindImp? 3421 assert exprs[i].didBindImp 3422 type = exprs[i].type.greatestCommonDenominatorWith(type) 3400 3423 if type.isSystemObjectClass # make dynamic if heterogenous and all non Object 3401 3424 allObj = all for expr in exprs get expr.type.isSystemObjectClass 3402 3425 if not allObj, type = .compiler.dynamicType 3403 if type inherits NilType, type = .compiler.defaultType 3404 _type = _makeTypeWith(type) 3405 3426 if type inherits NilType, type = .compiler.defaultType 3427 type = _makeTypeWith(type) 3428 return type 3429 3430 def _convertTypeNode as IType 3431 return _typeNode.realType 3432 3406 3433 def _makeTypeWith(type as IType) as IType is abstract 3407 3434 3408 3435 get brackets as List<of String> is abstract … … 3421 3448 3422 3449 class ListLit inherits SequenceLit is partial 3423 3450 3451 def _convertTypeNode as IType is override 3452 type = _typeNode.realType 3453 if not type.isGenericListLike 3454 sugg = ' Perhaps you meant "IList or List<of [type.name]>".' 3455 .throwError('An explicit list literal type must be of form "List<of someType>" not "[type.name]".[sugg]') 3456 return type 3457 3424 3458 def _makeTypeWith(type as IType) as IType is override 3425 3459 return .compiler.listOfType.constructedTypeFor([type]) 3426 3460 … … 3430 3464 3431 3465 class ArrayLit inherits SequenceLit is partial 3432 3466 3467 def _convertTypeNode as IType is override 3468 if not _typeNode inherits ArrayTypeIdentifier 3469 name = (_typeNode to AbstractTypeIdentifier).name 3470 sugg = ' Perhaps you meant "[name]\[]".' 3471 .throwError('An explicitly declared array type must be of form "someType\[]" not "[name]".[sugg]') 3472 return _typeNode.realType 3473 3433 3474 def _makeTypeWith(type as IType) as IType is override 3434 3475 return .typeProvider.arrayType(type) 3435 3476 … … 3454 3495 cue init(token as IToken, exprs as List<of Expr>) 3455 3496 base.init(token, exprs) 3456 3497 3498 cue init(token as IToken, exprs as List<of Expr>, typeNode as ITypeProxy?) 3499 base.init(token, exprs) 3500 _typeNode = typeNode 3501 3502 def _convertTypeNode as IType is override 3503 type = _typeNode.realType 3504 if not type.isSetLike 3505 sugg = ' Perhaps you meant "Set<of [type.name]>".' 3506 .throwError('An explicit set literal type must be of form "Set<of someType>" not "[type.name]".[sugg]') 3507 return type 3508 3457 3509 def _makeTypeWith(type as IType) as IType is override 3458 3510 return .compiler.setOfType.constructedTypeFor([type]) 3459 3511 … … 3478 3530 class DictLit inherits CompositeLiteral is partial 3479 3531 3480 3532 var _entries as List<of List<of Expr>> 3533 var _typeNode as ITypeProxy? 3481 3534 3482 3535 cue init(token as IToken, entries as List<of List<of Expr>>) 3483 3536 base.init(token) … … 3486 3539 assert entry.count == 2 3487 3540 _entries = entries 3488 3541 3542 cue init(token as IToken, entries as List<of List<of Expr>>, typeNode as ITypeProxy?) 3543 .init(token, entries) 3544 _typeNode = typeNode 3545 3489 3546 def addSubFields is override 3490 3547 base.addSubFields 3491 3548 .addField('entries', _entries) … … 3506 3563 return false 3507 3564 3508 3565 get entries from var 3566 3567 get isTentativelyTyped as bool is override 3568 # empty DictLit and type not explicitly set 3569 assert .didBindImp 3570 return _entries.count == 0 and _typeInferred 3509 3571 3510 3572 def replaceChild(find as INode, replace as INode) as bool 3511 3573 didReplace = base.replaceChild(find, replace) … … 3535 3597 hadError = true 3536 3598 if hadError 3537 3599 return 3600 if _typeNode # optional explicit type 3601 type = _typeNode.realType 3602 if not type.isDictionaryLike 3603 .throwError('An explicit dict literal type must be of form "Dictionary<of keyType,valueType>" not "[type.name]".') 3604 _type = type 3605 entType = _genEntriesType 3538 3606 if .type is nil 3539 entries = _entries 3540 if _entries.count == 0 3541 keyType = valueType = .compiler.defaultType 3542 else 3543 keyType = entries[0][0].type to ! 3544 valueType = entries[0][1].type to ! 3545 i = 1 3546 keysAllSysObject = valuesAllSysObject = true 3547 while i < entries.count 3548 keyType = entries[i][0].type.greatestCommonDenominatorWith(keyType) 3549 if not entries[i][0].type.isSystemObjectClass, keysAllSysObject = false 3550 valueType = entries[i][1].type.greatestCommonDenominatorWith(valueType) 3551 if not entries[i][1].type.isSystemObjectClass, valuesAllSysObject = false 3552 i += 1 3553 # make types dynamic if heterogenous and all non Object 3554 if keyType.isSystemObjectClass and not keysAllSysObject 3555 keyType = .compiler.dynamicType 3556 if valueType.isSystemObjectClass and not valuesAllSysObject 3557 valueType = .compiler.dynamicType 3558 if keyType inherits NilType, keyType = .compiler.defaultType 3559 if valueType inherits NilType, valueType = .compiler.defaultType 3560 _type = .compiler.dictionaryOfType.constructedTypeFor([keyType, valueType]) 3607 _type = entType 3608 _typeInferred = true 3609 else if _entries.count > 0 # type set from {...} as Dictionary<of TYPE,TYPE> 3610 if not entType.isAssignableTo(_type to !) 3611 .throwError('The common type of the dictionary literal expressions is [entType.name] which is not assignable to the explicitly declared type [_type.name].') 3612 3613 def _genEntriesType as IType 3614 entries = _entries 3615 if _entries.count == 0 3616 keyType = valueType = .compiler.defaultType 3617 else 3618 keyType = entries[0][0].type to ! 3619 valueType = entries[0][1].type to ! 3620 i = 1 3621 keysAllSysObject = valuesAllSysObject = true 3622 while i < entries.count 3623 keyType = entries[i][0].type.greatestCommonDenominatorWith(keyType) 3624 if not entries[i][0].type.isSystemObjectClass, keysAllSysObject = false 3625 valueType = entries[i][1].type.greatestCommonDenominatorWith(valueType) 3626 if not entries[i][1].type.isSystemObjectClass, valuesAllSysObject = false 3627 i += 1 3628 # make types dynamic if heterogenous and all non Object 3629 if keyType.isSystemObjectClass and not keysAllSysObject 3630 keyType = .compiler.dynamicType 3631 if valueType.isSystemObjectClass and not valuesAllSysObject 3632 valueType = .compiler.dynamicType 3633 if keyType inherits NilType, keyType = .compiler.defaultType 3634 if valueType inherits NilType, valueType = .compiler.defaultType 3635 entType = .compiler.dictionaryOfType.constructedTypeFor([keyType, valueType]) 3636 return entType 3561 3637 3562 3563 3638 class ToNilableOrNotExpr 3564 3639 is abstract, partial 3565 3640 inherits Expr -
Source/BackEndClr/SharpGenerator.cobra
4081 4081 is partial 4082 4082 4083 4083 def writeSharpDef(sw as CurlyWriter, parens as bool) is override 4084 innerType = (_type to Box).genericParams[0] 4084 # uuu 4085 try 4086 innerType = (_type to Box).genericParams[0] 4087 catch 4088 trace .idString, _type 4089 throw 4085 4090 sw.write('CobraCoreInternal.CobraImp.MakeList<[innerType.sharpRef]>(typeof([_type.sharpRef])') 4086 4091 if _exprs.count 4087 4092 sw.write(', ') -
Source/Types.cobra
1896 1896 return true 1897 1897 return false 1898 1898 1899 def isGenericListLike as bool 1900 """ 1901 Returns true if the receiver is like a generic List, such as being an actual generic List type or a nilable version thereof. 1902 The dynamic type will also return true because at compile-time, dynamic is considered to be like everything. 1903 Requires Node.getCompiler 1904 """ 1905 type = this 1906 if type inherits NilableType 1907 return type.nonNil.isGenericListLike 1908 tp = Node.getCompiler # 'tp' for 'type provider' 1909 genericList = tp.listOfType 1910 if type.isDescendantOf(genericList) 1911 return true 1912 if type inherits Box 1913 if type.genericDef is genericList 1914 return true 1915 genericIList = tp.ilistOfType 1916 if type.isDescendantOf(genericIList) 1917 return true 1918 if type inherits Box 1919 if type.genericDef is genericIList 1920 return true 1921 if type.isDynamic 1922 return true 1923 return false 1924 1925 def isSetLike as bool 1926 """ 1927 Returns true if the receiver is like a Set, such as being an actual Set type or a nilable version thereof. 1928 The dynamic type will also return true because at compile-time, dynamic is considered to be like everything. 1929 Requires Node.getCompiler 1930 """ 1931 type = this 1932 if type inherits NilableType 1933 return type.nonNil.isSetLike 1934 tp = Node.getCompiler # 'tp' for 'type provider' 1935 genericSet = tp.setOfType 1936 if type.isDescendantOf(genericSet) 1937 return true 1938 if type inherits Box 1939 if type.genericDef is genericSet 1940 return true 1941 # probably should also check/test for ISet as well, provide accessor in Compiler 1942 if type.isDynamic 1943 return true 1944 return false 1945 1899 1946 def isDictionaryLike as bool 1900 1947 """ 1901 1948 Returns true if the receiver is like a dictionary, such as being an actual dictionary type or a nilable version thereof. -
Source/CobraTokenizer.cobra
947 947 on 'skip', pass 948 948 on 'warning', pass 949 949 on 'warning-lax', pass 950 else, .throwError('Unrecognized compiler directive "[name]". ')950 else, .throwError('Unrecognized compiler directive "[name]". Known directives: "no-warnings" "args"') 951 951 return true 952 952 return false 953 953 -
Source/BinaryOpExpr.cobra
234 234 left = _left 235 235 if left inherits IdentifierExpr 236 236 if left.definition is nil 237 definition = LocalVar(left.token, .compiler.passThroughType).bindAll to LocalVar # CC: axe cast when supporting 'as this' 238 .compiler.codeMemberStack.peek.addLocal(definition) 239 left.setDefinition(definition) 240 left.type = definition.type 237 _inferVariableType(left, .compiler.passThroughType) 241 238 if _type is nil 242 239 _type = .compiler.passThroughType 243 240 throw … … 254 251 if right.hasError 255 252 # for a type inference situation, create a var of type passthrough for the left hand side 256 253 # to prevent superfluous errors in subsequent statements 257 definition = LocalVar(left.token, .compiler.passThroughType).bindAll to LocalVar # CC: axe cast when supporting 'as this' 258 .compiler.codeMemberStack.peek.addLocal(definition) 259 left.setDefinition(definition) 260 left.type = definition.type 254 _inferVariableType(left, .compiler.passThroughType) 261 255 _type = left.type 262 256 return 263 257 else … … 265 259 if left.name.startsWith('_') 266 260 .throwError('No class variable named "[left.name]" exists and local variable declarations cannot start with an underscore (_).') 267 261 inferredType = if(right.type inherits NilType, .compiler.nilableDynamicType, right.type) # `x = nil` for a new local var `x` is inferred as `dynamic?` 268 definition = LocalVar(left.token, inferredType).bindAll to LocalVar # CC: axe cast when supporting 'as this' 269 .compiler.codeMemberStack.peek.addLocal(definition) 270 left.setDefinition(definition) 271 left.type = definition.type 262 _inferVariableType(left, inferredType to !) 272 263 if left.definition is nil 273 264 left.throwUnknownIdError 274 265 if left.definition inherits LocalVar and (left.definition to LocalVar).name.startsWithNonLowerLetter … … 283 274 okay = true 284 275 if not okay 285 276 .throwError('Incompatible types. Cannot assign value of type [right.type.name] on the right to [left.type.name] on the left.') 277 278 # As a convenience, retype an implicitly typed emptyLit expression to the lvalue type 279 # Unneeded if lvalue is dynamic type 280 # var as TYPE? ... var = emptyLit and 281 # var as TYPE = emptyLit and 282 # .var = emptyLit 283 # emptyLit here is one of [], @[], {}, {:}, {,} 284 if not left.type.isDynamic and 285 (left inherits IdentifierExpr or left inherits AsExpr or left inherits DotExpr ) 286 if right inherits CompositeLiteral 287 if right.isTentativelyTyped 288 right.type = left.type.nonNil 289 286 290 _type = left.type 287 291 right.contextType = _type 288 292 293 # define a local var of the name of the Identifier Expr with the given type 294 def _inferVariableType(left as IdentifierExpr, type as IType) 295 definition = LocalVar(left.token, type).bindAll to LocalVar # CC: axe cast when supporting 'as this' 296 .compiler.codeMemberStack.peek.addLocal(definition) 297 left.setDefinition(definition) 298 left.type = definition.type 299 289 300 def checkAfterBindImp 290 301 base.checkAfterBindImp 291 302 if not .hasError and _left.definition inherits Param -
Source/CobraParser.cobra
3434 3434 def literalList as ListLit 3435 3435 token = .expect('LBRACKET') 3436 3436 exprs = .commaSepExprs('RBRACKET.', true) 3437 return ListLit(token, exprs) 3437 if .peek.which=='AS' # [] as List<of TYPE> 3438 .grab 3439 listType = .typeId 3440 return ListLit(token, exprs, listType) 3438 3441 3439 3442 def literalArray as ArrayLit 3440 3443 token = .expect('ARRAY_OPEN') 3441 3444 exprs = .commaSepExprs('RBRACKET.', true) 3442 return ArrayLit(token, exprs) 3445 if .peek.which=='AS' # @[] as TYPE[] 3446 .grab 3447 type = .typeId 3448 return ArrayLit(token, exprs, type) 3443 3449 3444 3450 def literalDictOrSet as CompositeLiteral 3445 3451 token = .expect('LCURLY') … … 3453 3459 on 'RCURLY' 3454 3460 .grab 3455 3461 _warning('Assuming empty dictionary, but please use "{:}" for empty dictionary or "{,}" for empty set') 3456 return DictLit(token, List<of List<of Expr>>()) 3462 if .optional('AS') # {:} as Dictionary<of TYPE,TYPE> 3463 type = .typeId 3464 return DictLit(token, List<of List<of Expr>>(), type) 3457 3465 expr = .expression 3458 3466 _spaceAgnostic 3459 3467 branch .peek.which … … 3484 3492 _spaceAgnostic 3485 3493 if .optional('RCURLY') 3486 3494 break 3487 return SetLit(token, exprs)3488 3495 else 3489 3496 .expect('COMMA') 3490 3497 _spaceAgnostic 3491 3498 .expect('RCURLY') 3492 return SetLit(token, List<of Expr>())3499 exprs = List<of Expr>() 3493 3500 3501 if .optional('AS') # {,} as Set<of TYPE> 3502 type = .typeId 3503 return SetLit(token, exprs, type) 3504 3494 3505 def _literalDict(token as IToken, expr as Expr?) as DictLit 3506 entries = List<of List<of Expr>>() 3495 3507 if expr 3496 3508 expectComma = false 3497 entries = List<of List<of Expr>>()3498 3509 first = true 3499 3510 while true 3500 3511 if first … … 3519 3530 value = .expression 3520 3531 entries.add([key, value]) 3521 3532 expectComma = true 3522 return DictLit(token, entries)3533 #return DictLit(token, entries) 3523 3534 else 3524 3535 .expect('COLON') 3525 3536 _spaceAgnostic 3526 3537 .expect('RCURLY') 3527 return DictLit(token, List<of List<of Expr>>()) 3538 3539 if .peek.which=='AS' # {:} as Dictionary<of TYPE,TYPE> 3540 .grab 3541 type = .typeId 3542 return DictLit(token, entries, type) 3528 3543 3529 3544 def nameExpr as NameExpr 3530 3545 nameToken = .expect('ID') -
Tests/820-errors/400-declarations/600-typed-literals/102-list-literal-error-assign1.cobra
1 # List literal explicit typing mismatch declared type and later assignment 2 class ListLitAs 3 def main is shared 4 l = [] as List<of MyClass> 5 l = ['hi'] # .error. Incompatible types. Cannot assign 6 CobraCore.noOp(l) 7 8 class MyClass 9 pass -
Tests/820-errors/400-declarations/600-typed-literals/200-array-literal-typed-error-type.cobra
1 # tests for explicit typing of Array literals 2 class ListLitAs 3 def main is shared 4 cerr = @[] as String # .error. array type must be of form "someType[]" 5 CobraCore.noOp(cerr) -
Tests/820-errors/400-declarations/600-typed-literals/203-array-literal-typed-assign1.cobra
1 # tests for explicit typing of Array literals 2 class ListLitAs 3 def main is shared 4 l = @[] as MyClass[] 5 l = @['hi'] # .error. Incompatible types. Cannot assign value of type String[] on the right to MyClass[] on the left 6 CobraCore.noOp(l) 7 8 class MyClass 9 pass 10 -
Tests/820-errors/400-declarations/600-typed-literals/101-list-literal-error-assignable.cobra
1 # List literal explicit typing mismatch expression types and declared type 2 class ListLitAs 3 def main is shared 4 d = [1,2,3] as List<of String> # .error. List<of int> which is not assignable to the explicitly declared type List<of String> 5 CobraCore.noOp(d) -
Tests/820-errors/400-declarations/600-typed-literals/304-set-literal-typed-error-assign2.cobra
1 # tests for explicit typing of Set literals 2 class SetLitAs 3 def main is shared 4 l = {'hi',} as Set<of MyClass> # .error. The common type of the sequence of expressions is Set<of String> which is not assignable to the explicitly declared type Set<of MyClass> 5 CobraCore.noOp(l) 6 7 class MyClass 8 pass 9 -
Tests/820-errors/400-declarations/600-typed-literals/400-dict-literal-typed-error-type.cobra
1 # tests for explicit typing of Dict literals 2 class DictLitAs 3 def main is shared 4 d = {:} as String # .error. dict literal type must be of form "Dictionary<of keyType,valueType>" 5 CobraCore.noOp(d) 6 -
Tests/820-errors/400-declarations/600-typed-literals/204-array-literal-types-error-assign2.cobra
1 # tests for explicit typing of Array literals expr list type mismatch to declared User type 2 class ListLitAs 3 def main is shared 4 l = @['hi'] as MyClass[] # .error. String[] which is not assignable to the explicitly declared type MyClass[] 5 CobraCore.noOp(l) 6 7 class MyClass 8 pass 9 -
Tests/820-errors/400-declarations/600-typed-literals/100-list-literal-error-type.cobra
1 # List literal explicit typing unreasonable declared type 2 class ListLitAs 3 def main is shared 4 cerr = [] as String # .error. An explicit list literal type must be of form "List<of someType>" not "String" 5 CobraCore.noOp(cerr) -
Tests/820-errors/400-declarations/600-typed-literals/402-dict-literal-typed-error-assign.cobra
1 # tests for explicit typing of Dict literals 2 class DictLitAs 3 def main is shared 4 d = {1:1, 2:1, 3:1} as Dictionary<of int,String> # .error. not assignable 5 CobraCore.noOp(d) 6 -
Tests/820-errors/400-declarations/600-typed-literals/403-dict-literal-typed-error-assign.cobra
1 # tests for explicit typing of Dict literals 2 class DictLitAs 3 def main is shared 4 d = {:} as Dictionary<of int, MyClass> 5 d = { 'hi':MyClass() } #.error. Incompatible types. Cannot assign value of type Dictionary<of String,MyClass> on the right to Dictionary<of int,MyClass> on the left 6 CobraCore.noOp(d) 7 8 class MyClass 9 pass -
Tests/820-errors/400-declarations/600-typed-literals/404-dict-literal-typed-error-assign.cobra
1 # tests for explicit typing of Dict literals 2 class DictLitAs 3 def main is shared 4 d = {'hi':10} as Dictionary<of int,MyClass> #.error. Dictionary<of String,int> which is not assignable to the explicitly declared type Dictionary<of int,MyClass>. 5 CobraCore.noOp(d) 6 #l1 = {'hi':10} as Dictionary<of String,MyClass> 7 8 class MyClass 9 pass -
Tests/820-errors/400-declarations/600-typed-literals/300-set-literal-typed-error-type.cobra
1 # tests for explicit typing of Set literals 2 class SetLitAs 3 def main is shared 4 c = {,} as String # .error. set literal type must be of form "Set<of someType>" 5 CobraCore.noOp(c) 6 -
Tests/820-errors/400-declarations/600-typed-literals/405-dict-literal-typed-error-assign.cobra
1 # tests for explicit typing of Dict literals 2 class DictLitAs 3 def main is shared 4 d = {'hi':10} as Dictionary<of String,MyClass> # .error. The common type of the dictionary literal expressions is Dictionary<of String,int> which is not assignable to the explicitly declared type Dictionary<of String,MyClass> 5 CobraCore.noOp(d) 6 7 class MyClass 8 pass -
Tests/820-errors/400-declarations/600-typed-literals/103-list-literal-error-assign.cobra
1 # List literal explicit typing mismatch expression and declared User type 2 class ListLitAs 3 def main is shared 4 l = ['hi'] as List<of MyClass> #.error. not assignable 5 CobraCore.noOp(l) 6 7 class MyClass 8 pass -
Tests/820-errors/400-declarations/600-typed-literals/301-set-literal-typed-assignable.cobra
1 # tests for explicit typing of Set literals 2 class SetLitAs 3 def main is shared 4 d = {1,2,3} as Set<of String> # .error. not assignable 5 CobraCore.noOp(d) -
Tests/820-errors/400-declarations/600-typed-literals/303-set-literal-types-error-assign1.cobra
1 # tests for explicit typing of Set literals 2 class SetLitAs 3 def main is shared 4 l = {,} as Set<of MyClass> 5 l = {'hi'} # .error. Cannot assign value of type Set<of String> on the right to Set<of MyClass> on the left 6 CobraCore.noOp(l) 7 8 class MyClass 9 pass -
Tests/820-errors/400-declarations/600-typed-literals/201-array-literal-typed-error-assignable.cobra
1 # tests for explicit typing of Array literals 2 class ListLitAs 3 def main is shared 4 d = @[1,2,3] as String[] # .error. not assignable to the explicitly declared type 5 CobraCore.noOp(d) -
Tests/820-errors/400-declarations/600-typed-literals/401-dict-literal-typed-error-type1.cobra
1 # tests for explicit typing of Dict literals 2 class DictLitAs 3 def main is shared 4 d = {:} as List<of int> # .error. dict literal type must be of form "Dictionary<of keyType,valueType>" 5 CobraCore.noOp(d) -
Tests/300-type-inference/900-literals/600-explicitly-typed/200-array-literal-typed.cobra
1 # tests for explicit typing of Array literals 2 class ListLitAs 3 def main is shared 4 # below array typenames specific to .Net family 5 elObject = r'System.Object[]' 6 elString = r'System.String[]' 7 elInt = r'System.Int32[]' 8 elMyClass= r'MyClass[]' 9 10 a = @[] 11 #print a.typeOf 12 assert elObject in a.typeOf.toString 13 assert a.length ==0 14 15 b = @[] as int[] 16 #print b.typeOf 17 assert elInt in b.typeOf.toString 18 assert b.length ==0 19 b = @[3,4] 20 assert b == @[3,4] 21 22 c = @[] as String[] 23 assert elString in c.typeOf.toString 24 c = @['My'] 25 assert c == @['My'] 26 c = @['a', 'b'] 27 assert c == @['a', 'b'] 28 29 l = @[] as MyClass[] 30 assert elMyClass in l.typeOf.toString 31 l = @[MyClass(), MyClass()] 32 assert l.length == 2 33 34 lf = @[FunkClass()] as MyClass[] 35 assert elMyClass in lf.typeOf.toString 36 assert lf.length == 1 37 assert 'FunkClass' in lf[0].typeOf.toString 38 39 class MyClass 40 pass 41 42 class FunkClass inherits MyClass 43 def wotAmI 44 print "I'm a funkyClass" 45 -
Tests/300-type-inference/900-literals/600-explicitly-typed/502-assign-empty-lit-to-property.cobra
1 class A 2 3 cue init 4 base.init 5 .stuff = {:} 6 7 pro stuff from var as Dictionary<of String, int> 8 9 def main 10 pass -
Tests/300-type-inference/900-literals/600-explicitly-typed/400-dict-literal-typed.cobra
1 # tests for explicit typing of Dict literals 2 class DictLitAs 3 def main is shared 4 # below element typenames specific to .Net family 5 elObject = r'[System.Object,System.Object]' 6 elString = r'[System.String,System.Int32]' 7 elInt = r'[System.Int32,System.String]' 8 elMyClass= r'[System.String,MyClass]' 9 10 a = {:} 11 #print a.typeOf 12 assert elObject in a.typeOf.toString 13 assert a.count ==0 14 15 b0 = {} as Dictionary<of int,String> #.warning. Assuming empty dictionary 16 #print b0.typeOf 17 assert elInt in b0.typeOf.toString 18 assert b0.count ==0 19 b0 = {3:'x0',4:'y0'} 20 assert b0.count == 2 21 assert b0[3] == 'x0' and b0[4] == 'y0' 22 23 b = {:} as Dictionary<of int,String> 24 #print b.typeOf 25 assert elInt in b.typeOf.toString 26 assert b.count ==0 27 b = {3:'x',4:'y'} 28 assert b.count == 2 29 assert b[3] == 'x' and b[4] == 'y' 30 31 c = {:} as Dictionary<of String, int> 32 assert elString in c.typeOf.toString 33 c['My'] = 1 34 assert c.count ==1 35 assert c['My'] == 1 36 c = {'a':1, 'b':2} 37 assert c.count == 2 38 assert c['a'] == 1 and c['b'] == 2 39 40 c1 = {'aa':1, 'bb':2} as Dictionary<of String, int> 41 assert elString in c1.typeOf.toString 42 assert c1.count == 2 43 assert c1['aa'] == 1 and c1['bb'] == 2 44 45 l = {:} as Dictionary<of String, MyClass> 46 assert elMyClass in l.typeOf.toString 47 l = {'cl0':MyClass(), 'cl1':MyClass()} 48 assert l.count == 2 49 50 fcls = FunkClass() 51 lf = {'mc':fcls} as Dictionary<of String, MyClass> 52 assert elMyClass in lf.typeOf.toString 53 assert lf.count == 1 54 assert lf['mc'] is fcls 55 #print 'done' 56 57 class MyClass 58 pass 59 60 class FunkClass inherits MyClass 61 def wotAmI 62 print "I'm a funkyClass" 63 -
Tests/300-type-inference/900-literals/600-explicitly-typed/102-list-literal-assigned-to-dynamic.cobra
1 class A 2 3 var _d as dynamic? 4 5 def foo 6 _d = [] 7 8 def bar 9 a as dynamic? 10 a = [] 11 CobraCore.noOp(a) 12 13 def main 14 .foo 15 .bar -
Tests/300-type-inference/900-literals/600-explicitly-typed/100-list-literal-typed.cobra
1 # tests for explicit typing of List literals 2 class ListLitAs 3 def main is shared 4 # below element typenames specific to .Net family 5 elObject = r'[System.Object]' 6 elString = r'[System.String]' 7 elInt = r'[System.Int32]' 8 elMyClass= r'[MyClass]' 9 10 a = [] 11 assert elObject in a.typeOf.toString 12 assert a.count ==0 13 14 b = [] as List<of int> 15 #print b.typeOf 16 assert elInt in b.typeOf.toString 17 assert b.count ==0 18 b = [3,4] 19 assert b == [3,4] 20 21 c = [] as List<of String> 22 assert elString in c.typeOf.toString 23 c.add('My') 24 assert c == ['My'] 25 c = ['a', 'b'] 26 assert c == ['a', 'b'] 27 28 l = [] as List<of MyClass> 29 assert elMyClass in l.typeOf.toString 30 l = [MyClass(), MyClass()] 31 assert l.count == 2 32 33 lf = [FunkClass()] as List<of MyClass> 34 assert elMyClass in lf.typeOf.toString 35 assert lf.count == 1 36 assert 'FunkClass' in lf[0].typeOf.toString 37 38 #TODO: IList 39 #ib = [] as IList<of > 40 41 class MyClass 42 pass 43 44 class FunkClass inherits MyClass 45 def wotAmI 46 print "I'm a funkyClass" 47 -
Tests/300-type-inference/900-literals/600-explicitly-typed/300-set-literal-typed.cobra
1 # tests for explicit typing of Set literals 2 class SetLitAs 3 def main is shared 4 # below element typenames specific to .Net family 5 elObject = r'[System.Object]' 6 elString = r'[System.String]' 7 elInt = r'[System.Int32]' 8 elMyClass= r'[MyClass]' 9 10 a = {,} 11 assert elObject in a.typeOf.toString 12 assert a.count ==0 13 14 b = {,} as Set<of int> 15 #print b.typeOf 16 assert elInt in b.typeOf.toString 17 assert b.count ==0 18 b = {3,4} 19 assert b == {3,4} 20 21 c = {,} as Set<of String> 22 assert elString in c.typeOf.toString 23 c.add('My') 24 assert c == {'My',} 25 c = {'a', 'b'} 26 assert c == {'a', 'b'} 27 28 l = {,} as Set<of MyClass> 29 assert elMyClass in l.typeOf.toString 30 l = {MyClass(), MyClass()} 31 assert l.count == 2 32 33 fcls = FunkClass() 34 lf = {fcls,} as Set<of MyClass> 35 assert elMyClass in lf.typeOf.toString 36 assert lf.count == 1 37 assert lf.contains(fcls) 38 39 class MyClass 40 pass 41 42 class FunkClass inherits MyClass 43 def wotAmI 44 print "I'm a funkyClass" 45 -
Tests/300-type-inference/900-literals/600-explicitly-typed/500-assign-empty-lit.cobra
1 # Allow untyped empty literals to be assigned to explicitly typed variable - ticket 230 2 use System.Text.RegularExpressions 3 4 class DotNet_RETypes 5 # Regexp strings to match Types specific to .Net family 6 var reListOfInt = r'List.*\[[A-Za-z.]*Int32\]' 7 var reSetOfInt = r'Set.*\[[A-Za-z.]*Int32\]' 8 var reArrayOfInt = r'Int32\[\]' 9 var reDictOfStringInt = r'Dictionary.*\[[A-Za-z.]*String,.*Int32\]' 10 var reDictOfStringString= r'Dictionary.*\[[A-Za-z.]*String,.*String\]' 11 12 class AssignEmptyList 13 14 def main 15 .dclLaterAssignEmpty 16 .dclAndAssign 17 18 def dclLaterAssignEmpty 19 20 #if CobraCore.compileTarget =='.Net' 21 m = DotNet_RETypes() 22 #else if CobraCore.compileTarget =='Java' 23 # m = Java_RETypes() 24 #... 25 26 # List 27 alist as List<of int>? 28 alist = [] 29 .chkPrintMatch(alist.typeOf, m.reListOfInt) 30 #print alist 31 #print alist.typeOf 32 33 # set 34 aset as Set<of int>? 35 aset = {,} 36 .chkPrintMatch(aset.typeOf, m.reSetOfInt) 37 #print aset 38 #print aset.typeOf 39 40 # array 41 arr as int[]? 42 arr = @[] 43 .chkPrintMatch(arr.typeOf, m.reArrayOfInt) 44 #print arr 45 #print arr.typeOf 46 47 # dict 48 ad as Dictionary<of String, int>? 49 ad = {} #.no-warnings. 50 .chkPrintMatch(ad.typeOf, m.reDictOfStringInt) 51 #print ad 52 #print ad.typeOf 53 54 ad1 as Dictionary<of String, String>? 55 ad1 = {:} 56 #print ad1 57 #print ad1.typeOf 58 .chkPrintMatch(ad1.typeOf, m.reDictOfStringString) 59 60 61 def dclAndAssign 62 #if CobraCore.compileTarget =='.Net' 63 m = DotNet_RETypes() 64 #else ... 65 66 alist as List<of int> = [] 67 .chkPrintMatch(alist.typeOf, m.reListOfInt) 68 #print alist 69 #print alist.typeOf 70 71 # set 72 aset as Set<of int> = {,} 73 .chkPrintMatch(aset.typeOf, m.reSetOfInt) 74 #print aset 75 #print aset.typeOf 76 77 # array 78 arr as int[] = @[] 79 .chkPrintMatch(arr.typeOf, m.reArrayOfInt) 80 #print arr 81 #print arr.typeOf 82 #arr=@[1,2] 83 #print arr 84 85 # dict 86 ad as Dictionary<of String, int> = {} #.no-warnings. 87 .chkPrintMatch(ad.typeOf, m.reDictOfStringInt) 88 #print ad 89 #print ad.typeOf 90 91 ad1 as Dictionary<of String, String> = {:} 92 .chkPrintMatch(ad1.typeOf, m.reDictOfStringString) 93 #print ad1 94 #print ad1.typeOf 95 96 97 def chkPrintMatch(t, ptn as String) 98 using sw = StringWriter() 99 print to sw 100 print t 101 assert Regex.isMatch(sw.toString, ptn) 102 -
Developer/IntermediateReleaseNotes.text
73 73 * The compiler recognizes the interfaces of primitive types such as `int` implementing `IComparable<of T>`. 74 74 75 75 76 * Fixed: Make "a = []" do the right thing when a is of a known type: ticket:230 77 Includes allowing explicit typing of Composite Literals 78 e.g avar = [] as List<of RedBugs> 79 avar as List<of RedBugs> = [] 80 81 shapes = {} as Set<of Shapes> 82 shapes as Set<of Shapes> = {} 83 shapes = {Circle()} as Set<of Shapes> # otherwise infer as Set<of Circle> 84 85 iArr = @[] as int[] 86 iArr as int[] = @[] 87 88 d = {:} as Dictionary<of String, int> 89 d as Dictionary<of String, int> = {:} 90 76 91 ================================================================================ 77 92 Library 78 93 ================================================================================