www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9443] New: Struct literal syntax in associative array literals too

http://d.puremagic.com/issues/show_bug.cgi?id=9443

           Summary: Struct literal syntax in associative array literals
                    too
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-02-02 13:43:27 PST ---
Array literals support the {} syntax to create structs, while associative array
literals don't:


struct Foo { int x; }
void main() {
    auto a1 = [Foo(10), Foo(20)]; // OK
    Foo[] a2 = [{10}, {20}]; // OK
    auto aa1 = [Foo(1): Foo(10), Foo(2): Foo(20)]; // OK
    Foo[Foo] aa2 = [{1}: {10}, {2}: {20}]; // Line6, error
}



DMD 2.062alpha gives:

temp.d(6): Error: not an associative array initializer


That syntax is partially supported by Go language maps:

package main
import "fmt"
type Foo struct { x int }
func main() {
    aa := map[int]Foo {1: {10}, 2: {20}}
    fmt.Println(aa[2])
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 02 2013