Closed HertzDevil closed 4 years ago
Sorry for the delay! Will look into this today or tomorrow at last. I think the feature itself is awesome š
Does this work with copied structs?
lib struct
s come with this capability already, as do record
structs. The major difference is that lib struct
s permit zero initialization if some of the fields are omitted, because their constructors are implemented as macros:
lib Binding
struct Point
x : Int32
y : Int32
end
end
# the following expands to:
# __temp_123 = Binding::Point.new
# __temp_123.x = 1
# __temp_123
Binding::Point.new(x: 1).y # => 0
# the following expands to:
# struct RecPoint
# def initialize(@x, @y = 5) end
# end
record RecPoint, x : Int32, y : Int32 = 5
# this PR would generate: (provided C++14 rules are used)
struct MyPoint
def initialize(*, x : Int32, y : Int32 = 5) end
end
The second difference is both lib struct
s and this PR require named arguments, whereas record
allows positional ones.
Thank you š
With this PR, wrapper classes of aggregate types may be created using a familiar record syntax, like
lib struct
s:Some aggregate types in Qt are
QPainterPath::Element
and theQStyleOption
hierarchy. Wrapping these types as Crystal classes rather thanlib struct
s means their methods no longer have to be reimplemented on the Crystal side (this is generally true for "structs" in many C++ APIs).The rules for aggregate initialization have changed in every C++ version. This PR follows the C++11 rules. The rules can be relaxed if Bindgen uses a more recent C++ standard requirement:
I ran the tests with the default value check disabled and
-std=c++14
added to the spec runner options, and this seemed to work out of the box.Point {.y = 2}
, it is possible to initialize C++ unions from any variant member other than the first. Without this, unions wouldn't really benefit much from aggregate initialization.