Replies: 4 comments 1 reply
-
C3 already has type inference for { ... } everywhere, so you can do Point* p = (Point){}.init(1, 2); Not a beauty, but that's where it's currently at. Other experiments didn't pan out so far. |
Beta Was this translation helpful? Give feedback.
-
For the
I really dont like the 2 step |
Beta Was this translation helpful? Give feedback.
-
Yes, I especially dont like the first line that defines an object with any initialization.
If
Yes these are called compound literals
I dont like the second one, I would prefer this alternative if
All of these are good, and this one too:
Taking the address of a compound literal is actually allowed in C so
Passing objects where pointers are expected is not allowed, but passing a pointer to a derived object should be allowed (implicit cast to base class), whether the object is defined as a compound literal or not. |
Beta Was this translation helpful? Give feedback.
-
I think there are 2 different uses, one is initialization by explicit members
The other is using a function, hiding specifics/complexity:
For more complex structs, the 2nd option would be better, the shorthand notation is more used for tuples and such. Your option for init-by-function:
might not be that hard to parse. This could of course be used as param/return as:
it would have to be defined as a static-type-function then. The init name would be nothing special, so not a default constructor, just some type-function you define yourself |
Beta Was this translation helpful? Give feedback.
-
Currently the pattern most often used in C2 to pass a struct by value is:
or alternatively:
More recent versions of C allow:
calc((Point){ 1, 2 });
Having something like this would allow for more concise code. My idea is to toy with some syntax proposals first, before implementing it.
There are some different use-cases to handle then:
initialization
assignment/param/return
There are some restrictions:
Beta Was this translation helpful? Give feedback.
All reactions