3 ms·
It's amazing they couldn't figure out how to get f(.{}) down to just f({}). Like here is this brace enclosed thing being matched against the argument type.
by kazinator 2y ago
It's amazing they couldn't figure out how to get f(.{}) down to just f({}). Like here is this brace enclosed thing being matched against the argument type.
- 8n4vidtmkvmk 2y agoThat's how it is in c++ so I'm used to it now, but I don't think I mind the dot. It differentiates a block scope from a struct initializer.
- akira2501 2y agoIn C's grammar you would need to wrap the {} as ({}) in order to get into a block expression scope from an argument list.
- kazinator 2y agoThat's GNU C, not standard C. Standard C has no such feature as blocks being expressions. Compound literals are grotesque. The braces have to be proceeded by cast syntax indicating the type. It could be subject to inference. Maybe the current draft has something.
- danhau 2y agoI actually find compound literals quite pleasing, visually. Parentheses, brackets and braces I find can help a lot in guiding your eyes though code. They make it more „regular“, or lessen the „entropy“ (physicists, please don‘t stone me). For this reason I‘ve come to dislike Rust’s braceless if-syntax, after being a die-hard fan. With parentheses it just reads better.
- flohofwoe 2y agoC99 has compound literals which allows you to call functions that take struct values or pointers to structs like this: my_func((my_type){ .x = 1, .y = 2 }); my_func(&(my_type){ .x = 1, .y = 2 });
- samatman 2y agoThose mean different things. f(.{}) is calling a function, which takes a struct, using an in-place constructor which provides the default arguments for the necessary struct type. f({}) is passing an instance of `void` to a function with a `void` type parameter. Do you need to do this, specifically? No. But you can. fn voidPass(v: void) void { _ = v; } test "void pass" { voidPass({}); }
- kristoff_it 2y agoYou do need to pass a void value if you use a hashmap as a set (eg StringHashMap(void)) Adding an entry requires you to pass a void value: map.put("hi", {});
- NotACracker 2y agoYeah, they designed a whole language, but they surely couldn't remove that damned dot. Every single time, I am telling you, every single time I must write an anonymous struct using that dot, I am becoming totally confused! Me too buddy, me too! Edit: I should put an /s here, am I right?