Type Annotations¶
# Simple types
x: int = 42
name: str = "Alice"
flag: bool = True
pi: float = 3.14159
# Type inference (annotation optional when initializer present)
y = 42 # Inferred as int
pi = 3.14159 # Inferred as float
Implementation - ✅ Native - Direct mapping to C# type declarations.
Nullability and Optional Type Syntax¶
T # Non-nullable type
T? # Optional[T] — safe tagged union (Sharpy-native)
T | None # C# nullable — .NET interop only
T !E # Result[T, E] — for return type annotations
T?is syntactic sugar forOptional[T]. See Optional Type.T | Noneis the only valid inline union form. No free unions likeint | str. See Nullable Types.T !Eis syntactic sugar forResult[T, E], recommended for top-level return types. See Result Type.
Shorthand Syntax¶
Sharpy supports shorthand syntax for common collection types. See Type Annotation Shorthand for details.