Skip to content

Commit

Permalink
fix Issue 481 - Letting compiler determine length for fixed-length ar…
Browse files Browse the repository at this point in the history
…rays
  • Loading branch information
9rnsr committed Jan 29, 2015
1 parent 7c70d76 commit 42b7679
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
55 changes: 35 additions & 20 deletions declaration.dd
Expand Up @@ -136,6 +136,15 @@ $(GNAME BasicType2X):
$(D delegate) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT)
$(D function) $(GLINK2 function, Parameters) $(GLINK2 function, FunctionAttributes)$(OPT)

$(GNAME BasicType3):
$(GLINK BasicType3X) $(GLINK BasicType3)$(OPT)

$(GNAME BasicType3X):
$(D *)
$(D [ ])
$(D [) $(VEXPRESSION) $(D ])
$(D [) $(GLINK Type) $(D ])

$(GNAME IdentifierList):
$(I Identifier)
$(I Identifier) $(D .) $(I IdentifierList)
Expand Down Expand Up @@ -276,45 +285,52 @@ $(H3 $(LEGACY_LNAME2 AutoDeclaration, auto-declaration, Implicit Type Inference)

$(GRAMMAR
$(GNAME AutoDeclaration):
$(GLINK StorageClasses) $(I AutoDeclarationX) $(D ;)
$(GLINK StorageClasses) $(GLINK BasicType3)$(OPT) $(I AutoDeclarationX) $(D ;)

$(GNAME AutoDeclarationX):
$(I Identifier) $(GLINK2 template, TemplateParameters)$(OPT) $(D =) $(GLINK Initializer)
$(I AutoDeclarationX) $(D ,) $(I Identifier) $(GLINK2 template, TemplateParameters)$(OPT) $(D =) $(GLINK Initializer)
)

$(P If a declaration starts with a $(I StorageClass) and has
$(P If a declaration starts with a $(I StorageClass) and has
a $(I NonVoidInitializer) from which the type can be inferred,
the type on the declaration can be omitted.
)

----------
static x = 3; // x is type int
auto y = 4u; // y is type uint
----------
static x = 3; // x is type int
auto y = 4u; // y is type uint

auto s = "string"; // s is type immutable(char)[]
auto s = "string"; // s is type immutable(char)[]

class C { ... }
class C { ... }

auto c = new C(); // c is a handle to an instance of class C
----------
auto c = new C(); // c is a handle to an instance of class C
----------
)

$(P The $(I NonVoidInitializer) cannot contain forward references
$(P The $(I NonVoidInitializer) cannot contain forward references
(this restriction may be removed in the future).
The implicitly inferred type is statically bound
to the declaration at compile time, not run time.
)

$(P An $(GLINK2 expression, ArrayLiteral)
is inferred to be a dynamic array
type rather than a static array:)
)

---
auto v = ["hello", "world"]; // type is string[], not string[2]
---
$(P An $(GLINK2 expression, ArrayLiteral) is inferred to be a dynamic array
type rather than a static array:

---
auto v = ["hello", "world"]; // type is string[], not string[2]
---
)

$(P $(I AutoDeclaration) can restrict the inferred type partially, by using
$(GLINK BasicType3).

---
auto[$] a = ["hello", "world"]; // type is string[2]
const[] b = [1, 2]; // type is const(int)[]
immutable* c = new int(3); // type is immutable(int)*
---
)

$(H3 $(LNAME2 alias, Alias Declarations))

Expand Down Expand Up @@ -484,7 +500,6 @@ extern extern(C) int bar; // variable allocated outside this module with C linka
// (e.g. in a statically linked C library or another module)
---------------


$(H3 $(LNAME2 typeof, $(D typeof)))

$(GRAMMAR
Expand Down
11 changes: 10 additions & 1 deletion grammar.dd
Expand Up @@ -67,6 +67,15 @@ $(GNAME BasicType2X):
$(D delegate) $(GLINK Parameters) $(GLINK MemberFunctionAttributes)$(OPT)
$(D function) $(GLINK Parameters) $(GLINK FunctionAttributes)$(OPT)

$(GNAME BasicType3):
$(GLINK BasicType3X) $(GLINK BasicType3)$(OPT)

$(GNAME BasicType3X):
$(D *)
$(D [ ])
$(D [) $(VEXPRESSION) $(D ])
$(D [) $(GLINK Type) $(D ])

$(GNAME IdentifierList):
$(I Identifier)
$(I Identifier) $(D .) $(I IdentifierList)
Expand Down Expand Up @@ -1014,7 +1023,7 @@ $(GNAME AliasDeclarationX):

$(GRAMMAR
$(GNAME AutoDeclaration):
$(GLINK StorageClasses) $(I AutoDeclarationX) $(D ;)
$(GLINK StorageClasses) $(GLINK BasicType3)$(OPT) $(I AutoDeclarationX) $(D ;)

$(GNAME AutoDeclarationX):
$(I Identifier) $(GLINK TemplateParameters)$(OPT) $(D =) $(GLINK Initializer)
Expand Down

0 comments on commit 42b7679

Please sign in to comment.