Rendered at 14:18:04 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
aleksiy123 1 days ago [-]
I pretty much always prefer using an options struct as soon as there is more than one optional argument.
Comes out cleaner because overriding a default argument doesn’t force you to also do all the positional arguments in front of it.
Designated initializers make it look really nice imo. I feel like the brackets are no big deal.
Python has sort of the opposite when you need to use *kwargs.
DarkUranium 3 hours ago [-]
I use this all the time in C (specifically, C99 or later). I first saw it used in anger in `sokol_gfx`, and loved it. `FLECS` does something similar.
C makes it much better than C++ in that the designated initializes can be set in any order --- so I don't need to remember the often-arbitrary order of struct fields for the options struct.
I find it weird that C++ took this great C feature and kneecapped it ...
DarkUranium 3 hours ago [-]
Addendum: I've been wanting to make my own shading language (for a number of reasons), and this feature is one I'd definitely include.
Think (for example) using a pluggable PBR module in this way, where you give it a config/options struct instead of a weird combination of `#if`, runtime arguments, and predefined uniforms and/or function names.
delta_p_delta_x 1 days ago [-]
> I pretty much always prefer using an options struct
This is essentially what Vulkan does; there's a CreateInfo struct for every object creation or command function. And even then they managed to sort of mess it up, because they also have functions and objects suffixed with a '2', and the pNext extension mechanism.
seeknotfind 1 days ago [-]
If you're calling this across translation units, the calling convention will come with a performance penalty, but boy have we come full circle since pre-ANSI C required you to pass args as a struct. Much love - wish the language required struct and arg list to be the same thing. You can send a list of em and it'll work with algebraic data types for batching calls. The dream. CPU doesn't play nice though since structs aren't register shaped, but maybe they could be in a future calling convention.
DaiPlusPlus 1 days ago [-]
To me, “keyword arguments” means actual language keywords being used as arguments, like “minute” or “hour” in T-SQL’s DATEDIFF, for example: `SELECT DATEDIFF( hour, NOW(), someDateCol )`.
…but I think the author meant “named arguments”, like we have in C#, Swift, and Objective-C.
rileymat2 1 days ago [-]
Python calls them keyword arguments.
tonyarkles 1 days ago [-]
They do, but they're also not strictly required to be named explicitly:
That's a C99 feature, designated initializer. Hardly modern. Yes it was ported to C++ relatively late, but it happened in C++20.
rwmj 1 days ago [-]
Don't C++ designated initializers require you to initialize in struct order? That makes them kind of annoying to use.
greenbit 1 days ago [-]
IMO it renders them kind of pointless, as you can then just leave out the field names (keywords, in pythonese) and get the same effect. It's very disappointing that they're naught but comments without the punctuation.
ozgrakkurt 23 hours ago [-]
You can disable the lint for this if you don’t use constructors/destructors in your project or if you are sure you won’t do funky stuff in the struct initializer afaik
manwe150 1 days ago [-]
Wouldn’t c99 also make you name the type there (looking sort of like a cast), further straying from being just kwargs? I thought this was a c++ deduction feature for it to bind the initializer to whether method could take that list
asalahli 1 days ago [-]
There was a similar CppCon talk[0] in 2018. Highly recommended (by me, fwiw), as is any other talk by Richard Powell
I did this in my C project 7 years ago, as this is standard in C and gave a lot of readability, in fact more I guess... but a lot of preprocessor code too
ranger_danger 1 days ago [-]
Wouldn't that also mean you now need to define a new struct for every method that you want to do this with?
akoboldfrying 1 days ago [-]
Reminds me of an idea I had years ago, for implementing "named binary operator syntax" in C++ so that stuff like the following would work:
int x = 5 <xor> 3; // x = 6
The basic trick was to notice that this is really parsed as:
Yeah, I've first seen it over 15 years ago. Usually you use operator of the same priority as you'd like, and also #define xor &xor_i& to get all that detail out of sight.
Comes out cleaner because overriding a default argument doesn’t force you to also do all the positional arguments in front of it.
Designated initializers make it look really nice imo. I feel like the brackets are no big deal.
Python has sort of the opposite when you need to use *kwargs.
C makes it much better than C++ in that the designated initializes can be set in any order --- so I don't need to remember the often-arbitrary order of struct fields for the options struct.
I find it weird that C++ took this great C feature and kneecapped it ...
Think (for example) using a pluggable PBR module in this way, where you give it a config/options struct instead of a weird combination of `#if`, runtime arguments, and predefined uniforms and/or function names.
This is essentially what Vulkan does; there's a CreateInfo struct for every object creation or command function. And even then they managed to sort of mess it up, because they also have functions and objects suffixed with a '2', and the pNext extension mechanism.
…but I think the author meant “named arguments”, like we have in C#, Swift, and Objective-C.
They can be:
But you can also use them generically:https://docs.python.org/3/glossary.html?hl=en-GB#term-keywor...
0. https://youtu.be/Grveezn0zhU
EDIT: Thanks commenter hawkice for fixing my XOR arithmetic!
See https://en.cppreference.com/cpp/language/operator_alternativ...