Nested Types¶
Implementation status: ✅ Implemented
Sharpy supports nested type declarations — classes, structs, interfaces, and enums defined inside other type bodies. These map directly to C# nested types.
Syntax¶
class LinkedList:
@public
class Node:
value: int
next: LinkedList.Node?
def __init__(self, value: int):
self.value = value
self.next = None
head: LinkedList.Node?
def __init__(self):
self.head = None
Access Semantics¶
- Default access: Nested types default to
@private(matching C# nested type defaults, Axiom 1) - Use
@public,@protected,@internal, or@privatedecorators to change access - Nested types can access
@privatemembers of the enclosing type (matching C# semantics) - External code references nested types via dot notation:
Outer.Inner
Supported Nesting¶
All combinations of type-in-type nesting are supported:
| Nested Type | Inside Class | Inside Struct | Inside Interface |
|---|---|---|---|
| class | ✅ | ✅ | ✅ |
| struct | ✅ | ✅ | ✅ |
| interface | ✅ | ✅ | ✅ |
| enum | ✅ | ✅ | ✅ |
Arbitrary nesting depth is supported (class inside class inside class).
Type References¶
Use dot notation to reference nested types in annotations:
Construction¶
Nested type constructors use the qualified name:
C# Emission¶
public class LinkedList
{
private class Node // private by default
{
public int Value;
// ...
}
// ...
}
Restrictions¶
- Static nesting only: No Java-style inner class
outerreference - Nested types are statically nested — they do not capture an implicit reference to an enclosing instance