chore: add example to demonstrate some features

This commit is contained in:
2026-06-22 15:29:39 +02:00
parent 0ba0266bae
commit 7e5ea5e414
2 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
predicate in_range(min: float, max: float)(v: float) = min <= v & v <= max
predicate is_ratio = in_range(0, 1)
type Money = float
type Price[T <: Money] = T where _ >= 0
type EUR = Money
type USD = Money
type Reduction = float where is_ratio(_)

View File

@@ -0,0 +1,19 @@
from typing import TypeVar, cast
from demo_stubs import EUR, USD, Money, Price, Reduction
T = TypeVar("T", bound=Money)
def apply_reduction(amount: Price[T], reduction: Reduction) -> Price[T]:
return cast(Price[T], (1.0 - reduction) * amount)
a1 = cast(Price[EUR], 3.2)
a2 = cast(Price[USD], 10.4)
r1: Reduction = cast(Reduction, 0.2)
print(apply_reduction(a1, r1))
print(apply_reduction(a2, r1))
# a3 = a1 + a2