Replies: 3 comments 2 replies
-
Seconded. While our BDFL wants to discourage the use of functional paradigms, a syntax for anonymous structs would help a lot for this and some other use cases, and if kept sufficiently simple would still keep enough friction in place to deter heavy use of captures and callbacks. I propose something like the following syntax: cap := struct{?} { // The ? is inspired by the [?]T{} array syntax
x, y, // names two fields of the struct x and y, with the same types as the variables in scope, then sets the value of each to match the variables in scope. Captures by value.
&foo, // names the field foo, sets its type to ^type_of(foo), and then sets its value to &foo. Captures by pointer
mem := raw_data(buffer), // defines a field called 'mem', infers its type, and then sets its value to raw_data(buffer)
i: int, //defines a field called 'i' with type int set to zero
s: string = "flibbertygibbet", // does exactly what you'd expect
} It's a little richer than the magic function you proposed and would add some complexity to the parser, but it also allows some broader use cases beyond local captures since you could use it to namespace constants and other things. |
Beta Was this translation helpful? Give feedback.
-
Just to add a bit - Odin already has a similar language feature for |
Beta Was this translation helpful? Give feedback.
-
I think I'd like to see it as its own keyword, probably something like this: var := #capture{a, b, c, bar, baz} |
Beta Was this translation helpful? Give feedback.
-
I deal with a lot of Callback functions I find myself needing a little more helper function/syntax sugar in Odin to make it easier. Take the following example -
A simple proc that takes a user_data pointer and a callback function. I usually end up capturing variables to use them in the callback like the following -
The
using
keyword is really helpful. But currently one has to declare a struct and then capture them in two steps. Just combining these two steps into just one step would be very helpful.Like the following perhaps
Now this is still explicitly capturing the variables and the programmer has to manage all the lifetimes explicitly unlike C++. This is just creating a
struct
on the fly.Beta Was this translation helpful? Give feedback.
All reactions