Skip to content

Commit b03cfcc

Browse files
authored
Merge pull request #2813 from ntrel/nested-class
Improve Nested Classes section merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 1ca5e4d + a8fef37 commit b03cfcc

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

spec/class.dd

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,9 +1358,11 @@ void func()
13581358
}
13591359
------
13601360

1361+
$(H3 $(LNAME2 static-nested, Static Nested Classes))
1362+
13611363
$(P If a nested class has the $(D static) attribute, then it can not access
13621364
variables of the enclosing scope that are local to the stack or need a
1363-
`this`:)
1365+
`this` reference:)
13641366

13651367
------
13661368
class Outer
@@ -1394,9 +1396,11 @@ void func()
13941396
}
13951397
------
13961398

1399+
$(H3 $(LNAME2 nested-context, Context Pointer))
1400+
13971401
$(P Non-static nested classes work by containing an extra hidden member (called
13981402
the context pointer) that is the frame pointer of the enclosing function if it
1399-
is nested inside a function, or the $(D this) of the enclosing class's instance
1403+
is nested inside a function, or the $(D this) reference of the enclosing class's instance
14001404
if it is nested inside a class.)
14011405

14021406
$(P When a non-static nested class is instantiated, the context pointer
@@ -1425,9 +1429,9 @@ void func()
14251429
}
14261430
------
14271431

1432+
$(H3 $(LNAME2 nested-explicit, Explicit Instantiation))
14281433

1429-
1430-
$(P A $(I this) can be supplied to the creation of an
1434+
$(P A `this` reference can be supplied to the creation of an
14311435
inner class instance by prefixing it to the $(I NewExpression):
14321436
)
14331437

@@ -1454,14 +1458,16 @@ int bar()
14541458
}
14551459
---------
14561460

1457-
$(P Here $(D o) supplies the $(I this) to the outer class
1461+
$(P Here $(D o) supplies the `this` reference to the inner class
14581462
instance of $(D Outer).
14591463
)
14601464

1461-
$(P The property $(D .outer) used in a nested class gives the
1462-
$(D this) pointer to its enclosing class. If there is no enclosing
1463-
class context, $(D .outer) would return a pointer to enclosing
1464-
function frame with $(D void*).
1465+
$(H3 $(LNAME2 outer-property, `outer` Property))
1466+
1467+
$(P For a nested class instance, the $(D .outer) property is the
1468+
$(D this) reference for the enclosing class's instance. If there
1469+
is no enclosing class context, $(D .outer) would be a $(D void*)
1470+
to the enclosing function frame.
14651471
)
14661472

14671473
----
@@ -1484,16 +1490,16 @@ class Outer
14841490
void bar()
14851491
{
14861492
// x is referenced from nested scope, so
1487-
// bar makes a closure envronment.
1493+
// bar makes a closure environment.
14881494
int x = 1;
14891495

14901496
class Inner2
14911497
{
14921498
Outer getOuter()
14931499
{
14941500
x = 2;
1495-
// The Inner2 instance owns function frame of bar
1496-
// as static frame pointer, but .outer yet returns
1501+
// The Inner2 instance has access to the function frame
1502+
// of bar as a static frame pointer, but .outer returns
14971503
// the enclosing Outer class instance property.
14981504
return this.$(CODE_HIGHLIGHT outer);
14991505
}
@@ -1503,18 +1509,19 @@ class Outer
15031509
assert(i.getOuter() is this);
15041510
}
15051511

1512+
// baz cannot access an instance of Outer
15061513
static void baz()
15071514
{
1508-
// make a closure envronment
1515+
// make a closure environment
15091516
int x = 1;
15101517

15111518
class Inner3
15121519
{
15131520
void* getOuter()
15141521
{
15151522
x = 2;
1516-
// There's no accessible enclosing class instance, so
1517-
// .outer property returns the function frame of bar.
1523+
// There's no accessible enclosing class instance, so the
1524+
// .outer property returns the function frame of baz.
15181525
return this.$(CODE_HIGHLIGHT outer);
15191526
}
15201527
}
@@ -1525,29 +1532,48 @@ class Outer
15251532
}
15261533
----
15271534

1528-
$(H2 $(LNAME2 anonymous, Anonymous Nested Classes))
1535+
$(H3 $(LNAME2 anonymous, Anonymous Nested Classes))
15291536

15301537
$(P An anonymous nested class is both defined and instantiated with
15311538
a $(I NewAnonClassExpression):
15321539
)
15331540

15341541
$(GRAMMAR
15351542
$(GNAME NewAnonClassExpression):
1536-
$(D new) $(GLINK2 expression, AllocatorArguments)$(OPT) $(D class) $(I ClassArguments)$(OPT) $(GLINK SuperClass)$(OPT) $(GLINK Interfaces)$(OPT) $(GLINK2 struct, AggregateBody)
1543+
$(D new) $(GLINK2 expression, AllocatorArguments)$(OPT) $(D class) $(I ConstructorArgs)$(OPT) $(GLINK SuperClass)$(OPT) $(GLINK Interfaces)$(OPT) $(GLINK2 struct, AggregateBody)
15371544

1538-
$(GNAME ClassArguments):
1545+
$(GNAME ConstructorArgs):
15391546
$(D $(LPAREN)) $(GLINK2 expression, ArgumentList)$(OPT) $(D $(RPAREN))
15401547
)
15411548

15421549
which is equivalent to:
15431550

15441551
$(GRAMMAR
15451552
$(D class) $(GLINK_LEX Identifier) $(D :) $(I SuperClass) $(I Interfaces) $(I AggregateBody)
1546-
$(D new) $(D $(LPAREN))$(I ArgumentList)$(D $(RPAREN)) $(GLINK_LEX Identifier) $(D $(LPAREN))$(I ArgumentList)$(D $(RPAREN));
1553+
// ...
1554+
$(D new) $(I AllocatorArguments) $(I Identifier) $(I ConstructorArgs)
15471555
)
15481556

15491557
where $(I Identifier) is the name generated for the anonymous nested class.
15501558

1559+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1560+
---
1561+
interface I
1562+
{
1563+
void foo();
1564+
}
1565+
1566+
auto obj = new class I
1567+
{
1568+
void foo()
1569+
{
1570+
writeln("foo");
1571+
}
1572+
};
1573+
obj.foo();
1574+
---
1575+
)
1576+
15511577
$(SECTION2 $(LEGACY_LNAME2 ConstClass, const-class, $(ARGS Const, Immutable and Shared Classes)),
15521578

15531579
$(P If a $(I ClassDeclaration) has a $(D const), $(D immutable)

spec/template.dd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,8 @@ $(H3 $(LNAME2 implicit-nesting, Implicit Nesting))
13881388
}
13891389
----
13901390

1391+
$(P See also: $(DDSUBLINK spec/class, nested-explicit, Nested Class Instantiation).)
1392+
13911393
----
13921394
void main()
13931395
{

0 commit comments

Comments
 (0)