Wiki

Ticket #238: struct-init.patch

File struct-init.patch, 2.6 KB (added by hopscc, 14 years ago)
  • Source/BackEndClr/SharpGenerator.cobra

     
    15381538        if _initExpr 
    15391539            sw.write(' = ') 
    15401540            _initExpr.writeSharpDef(sw) 
    1541         else if .type.isReference and not .type.nonNil inherits GenericParam 
     1541        else if _canInitDefault  
    15421542            sharpInit = .type.sharpInit 
    15431543            if sharpInit.length 
    15441544                sw.write(' = ') 
    15451545                sw.write(sharpInit) 
    15461546        sw.write(';\n') 
     1547         
     1548    def _canInitDefault as bool 
     1549        # In C# struct members can't be explicitly initted on declaration 
     1550        if .compiler.boxStack.peek.sharpKeyWord == 'struct', return false 
     1551        return .type.isReference and not .type.nonNil inherits GenericParam 
     1552         
    15471553 
    1548  
    15491554class AbstractMethod 
    15501555    is partial 
    15511556 
  • Tests/140-structs/210-struct-init.cobra

     
     1# Test fix for Ticket:238. Default InitExprs not allowed in structs 
     2#struct-init.cobra(8): error: "St.Name": cannot have instance field initializers in structs (C#) 
     3#struct-init.cobra(19): error: "St1.X": cannot have instance field initializers in structs (C#) 
     4class XX 
     5    pass 
     6     
     7struct St 
     8    var name as String 
     9    var n as int 
     10     
     11    cue init(name as String, n as int) 
     12        .name = name 
     13        .n = n 
     14         
     15#   def toString as String is override 
     16#       return 'St(name=[.name], n=[.n])'    
     17     
     18struct St1 
     19    var x as XX 
     20    var n as int 
     21     
     22    cue init(x as XX, n as int) 
     23        .x = x 
     24        .n = n 
     25         
     26#   def toString as String is override 
     27#       return 'St(x=[.x], n=[.n])'  
     28         
     29class Entry 
     30    def main is shared 
     31        x = St('xxx', 10) 
     32        assert x.name == 'xxx' and x.n == 10 
     33         
     34        xx = XX() 
     35        x1 = St1(xx, 11) 
     36        assert x1.x == xx and x1.n == 11 
     37 
     38        # declare only and pack/unpack members explicitly 
     39        xa as St 
     40        #pack 
     41        xa.name = 'aaa' 
     42        xa.n = 12 
     43        #unpack 
     44        xa_name = xa.name 
     45        xa_n = xa.n 
     46        assert xa_name == 'aaa' and xa_n == 12 
  • Developer/IntermediateReleaseNotes.text

     
    477477* Fixed: File and line number are duplicated in some compiler error messages.  ticket:212 
    478478 
    479479* Fixed: Some invariants cause an internal error.  ticket:248 
     480 
     481* Fixed: Bad codegen for structs containing strings (ref types).  ticket:238