Created
July 31, 2012 15:01
-
-
Save stepancheg/3217638 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "library" function to declare a record | |
[Base] | |
recordExtend(static Base, ..fields) = ..RecordFields(Base), ..fields; | |
// "library" function to declare hierarchy | |
// should be overloaded for each subtype, type pair | |
[A, B] | |
Extends?(static A, static B) = false; | |
// "library" function that allows subtype-type pointer assignment | |
[A, B | Extends?(A, B)] | |
overload assign(ref to: Pointer[B], from: Pointer[A]) { | |
to = Pointer[B](from); | |
} | |
record ShapeBase ( | |
color: String, | |
opacity: Float, | |
); | |
record Circle = ..recordExtend(ShapeBase, | |
diameter: Float | |
); | |
record Rectangle = ..recordExtend(ShapeBase, | |
width: Int, | |
height: Int | |
); | |
overload Extends?(Circle, ShapeBase) = true; | |
overload Extends?(Rectangle, ShapeBase) = true; | |
main() { | |
var circle = Circle(); | |
circle.color = String("red"); | |
var circlePtr = &circle; | |
// declaring a variable of type Pointer[ShapeBase] | |
var shapePtr = null(ShapeBase); | |
// this is it: assignment from a pointer of subtype to a pointer of base type | |
shapePtr = circlePtr; | |
// print "red" | |
println(shapePtr^.color); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment