Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Rect {
- var width height: number;
- }
- struct Circle {
- var radius: number;
- }
- trait Shaped[T] {
- function get_area(value: ref T): number;
- }
- @instance
- trait Shaped[Rect] {
- function get_area(value: Rect): number {
- return value.width * value.height;
- }
- }
- @instance
- trait Shaped[Circle] {
- function get_area(value: Circle): number {
- return value.radius * value.radius * 3.1415;
- }
- }
- // Here starts the fun part
- struct Shape[@erase T] {
- trait Shaped[T];
- var val: T;
- }
- var shape: Shape[Circle] = {
- val = {
- radius=20
- // trait is automatic
- }
- };
- // `shape` acts exactly as if no @erase annotation
- // if we omit the type parameter, Shape becomes sizeless type
- // ref Shape is fair game
- var some_shape: ref Shape;
- some_shape = &shape; // this (type erasure) is allowed
- // this is allowed - some_shape.trait is used as an implementation
- get_area(some_shape.val);
Add Comment
Please, Sign In to add comment