New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: inferred struct field type via default value #2866
Comments
Does this make it harder to easily identify what is a decl and what is a regular assignment? Unlike in func / global scope there's no keyword to make it easily identifiable. |
Good point. My half-baked idea — prepend the dot like regular struct init: const ZeeAlloc = struct {
backing_allocator: *Allocator, // possibly always prepend?
.free_lists = [_]FreeList{FreeList.init()} ** size_buckets,
.page_size = usize(page_size),
.allocator = Allocator{
.reallocFn = realloc,
.shrinkFn = shrink,
},
} |
I don't know about this proposal. I think adding a new way of Specifically about the style-mismatch, this brings up implicit casting vs.
The latter style of nullables is rarely used currently. You could argue that |
The style mismatch already exists with regular variable declarations: var a: usize = 5;
var b = usize(5);
struct {
a: usize = 5,
b = usize(5),
} I just think the semantics are similar enough to warrant similar syntax. |
On the styling, I've been musing about dot-syntax to differentiate between field vs. namespace decls and dropping "comma" with some added benefits:
const Foo = struct {
.some_flag = false;
.arena_storage: std.heap.ArenaAllocator;
.arena: *std.mem.Allocator;
fn init(backing: *std.mem.Allocator) Foo {
return Foo{
.arena_storage = std.heap.ArenaAllocator.init(backing);
.arena = &.arena_storage;
};
}
}; this has the consequence of... changing how field initialization looks (commas now become semicolon): var a = Car{ .a = 1; .b = 2; .c = 3; };
var b = Car{ .a = 1; .b = 2; .c = a+b; }; |
I think the fields with default values could be inferred in the same way as variables:
The text was updated successfully, but these errors were encountered: