-
Notifications
You must be signed in to change notification settings - Fork 0
Object Oriented Programming
This guide covers classes, fields, methods, inheritance, polymorphism, and LP's compile-time checked private and protected access rules.
- Read Language Basics first.
LP classes use Python-like syntax with field declarations and methods.
class Entity:
x: float = 0.0
y: float = 0.0
def move(self, dx: float, dy: float) -> float:
self.x = self.x + dx
self.y = self.y + dy
return self.xCreate an object by calling the class name:
player = Player()The current examples use default field values rather than custom constructor methods.
Subclasses can inherit from a base class:
class Player(Entity):
health: int = 100Methods can be overridden in subclasses. The regression suite verifies inherited and overridden behavior.
LP checks member visibility at compile time.
-
private: accessible only inside the declaring class. -
protected: accessible inside the declaring class and its subclasses. - public: the default when no access modifier is present.
The current compiler enforces these rules for:
- field reads
- field writes
- method calls
This is the same style used by tests/regression/02_oop_game_engine.lp.
class Entity:
x: float = 0.0
y: float = 0.0
def move(self, dx: float, dy: float) -> float:
self.x = self.x + dx
self.y = self.y + dy
return self.x
class Player(Entity):
health: int = 100
name: str = "Hero"
def take_damage(self, amount: int) -> int:
self.health = self.health - amount
return self.healthclass Dog:
protected kind: str = "dog"
class Puppy(Dog):
def reveal_kind(self) -> str:
return self.kindThis pattern is verified by tests/regression/test_inheritance_access.lp.
class SecretBox:
private code: int = 7
box = SecretBox()
# box.code = 9The commented line should be treated as a compile-time error in the current compiler.
- Access control is compile-time enforced. If you try to touch a
privateorprotectedmember from the wrong scope, the compiler should reject it. -
protectedis designed for subclass access, not unrestricted same-file access. - The public docs focus on field defaults and methods. More elaborate object-construction patterns are possible in source, but they are not the center of the verified example set yet.
- Exported C APIs skip
privatefunctions and top-level names that start with_by convention.
- Expressions and Collections
- [Error Handling and Advanced Control Flow]
- Examples Cookbook
- Language Reference
Back to Home | GitHub Repo | Issues
- Home
- Installation and Setup
- First Programs
- Language Basics
- Quick Reference
- Troubleshooting
- Known Limitations
- Language Reference
- Expressions and Collections
- Object-Oriented Programming
- Error Handling
- Feature Overview
- Feature Status
- Runtime Modules
- Concurrency and Parallelism
- Parallel and GPU Computing
- Security Overview
- Security Reference
- Security Calling Patterns
- Native ASM is the default backend.
- On Windows, prefer
--gccfor verification. - Docs are written against verified current behavior.