Structs are required to have one field
Structs are awesome. Allowing empty structs would make them complete.
+1
#3192 related
This appears to be related to an LLVM limitation? If so, would it be too much to make empty structs take 1 byte (as we do for nil, for I think similar reasons)?
That would be okay with me. Mostly I just want to be able to write structs that are nothing but a destructor.
For reference, an empty struct in clang generates:
%struct.foo = type {}
I propose we do that.
@eholk use util::noncopyable. it is basically that, except it has a unit field for now.
I'm looking for the issue. While I'm trying to fix the issue, I found an ambiguity between empty struct vs. empty match statement.
With following code
match x{} {}
Two interpretation is possible, which is listed blow
match (x{}) {} //matching with newly-constructed empty struct
(match x{}) {} //matching with empty enum(or struct) x and then empty block
It seems that there is no such code in rust code base, but there is one test which uses empty match statement: https://github.com/mozilla/rust/blob/incoming/src/test/run-pass/issue-3037.rs
All other cases could be distinguished with look-ahead, but this can't be. One possible solution is wrapping with parentheses when matching with an uninhabited type.
enum what { }
fn match_with_empty(x: what) -> ~str {
match (x) { //use parentheses to remove the ambiguity
}
}
How do you think about the issue?
Would it be possible to solve the ambiguity here by not needing {} to construct an empty struct? That way a struct X{} could just be declared like let var: X = X; instead of let var: X = X{};, and it would just be match X { ... }.
It is possible to have an empty struct. You simply write struct X;. You can reference an instance of this type as X.