forward declaration
This commit is contained in:
41
examples/issue-01.sx
Normal file
41
examples/issue-01.sx
Normal file
@@ -0,0 +1,41 @@
|
||||
#import "modules/std.sx";
|
||||
|
||||
// Forward references: types, struct fields, methods, and free functions
|
||||
// can reference types declared later in the file.
|
||||
|
||||
// Free function referencing types declared later
|
||||
make_frame :: (e: EdgeInsets) -> Frame {
|
||||
Frame.{ x = e.left, y = e.top,
|
||||
w = 100.0 - e.left - e.right,
|
||||
h = 100.0 - e.top - e.bottom };
|
||||
}
|
||||
|
||||
// Struct with a field whose type is declared later
|
||||
Container :: struct {
|
||||
frame: Frame;
|
||||
insets: EdgeInsets;
|
||||
}
|
||||
|
||||
Frame :: struct {
|
||||
x, y, w, h: f32;
|
||||
|
||||
inset :: (self: Frame, insets: EdgeInsets) -> Frame {
|
||||
Frame.{ x = self.x + insets.left, y = self.y + insets.top,
|
||||
w = self.w - insets.left - insets.right,
|
||||
h = self.h - insets.top - insets.bottom };
|
||||
}
|
||||
}
|
||||
|
||||
EdgeInsets :: struct {
|
||||
top, left, bottom, right: f32;
|
||||
}
|
||||
|
||||
main :: () {
|
||||
e := EdgeInsets.{ top = 10.0, left = 10.0, bottom = 10.0, right = 10.0 };
|
||||
f := make_frame(e);
|
||||
r := f.inset(e);
|
||||
c := Container.{ frame = f, insets = e };
|
||||
print("{}", r.x);
|
||||
print(" {}", r.w);
|
||||
print(" {}", c.frame.x);
|
||||
}
|
||||
Reference in New Issue
Block a user