Back to News
Advertisement
Advertisement

⚡ Community Insights

Discussion Sentiment

80% Positive

Analyzed from 484 words in the discussion.

Trending Topics

#bar#int#left#struct#arguments#xor#python#foo#baz#kwargs

Discussion (18 Comments)Read Original on HackerNews

asalahli16 minutes ago
There was a similar CppCon talk[0] in 2018. Highly recommended (by me, fwiw), as is any other talk by Richard Powell

0. https://youtu.be/Grveezn0zhU

aleksiy123about 2 hours 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.

p0w3n3dabout 1 hour ago
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
seeknotfindabout 3 hours 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.
DaiPlusPlusabout 3 hours 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.

rileymat2about 3 hours ago
Python calls them keyword arguments.
tonyarklesabout 1 hour ago
They do, but they're also not strictly required to be named explicitly:

They can be:

    >>> def foo(bar=None):
    ...   print(bar)
    ...
    >>> foo()
    None
    >>> foo(bar="baz")
    baz
    >>> foo(bar="baz", baz="azp")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() got an unexpected keyword argument 'baz'
But you can also use them generically:

    >>> def bar(**kwargs):
    ...   print(kwargs)
    ...
    >>> bar()
    {}
    >>> bar(site="HN", user="tonyarkles")
    {'site': 'HN', 'user': 'tonyarkles'}
cenamusabout 2 hours ago
And I suppose Lisp started that with :key args
antonvsabout 2 hours ago
Python’s perversity is fractal.
akoboldfryingabout 3 hours ago
I think the author meant "keyword arguments", like they're called in Python, and which they mentioned in the first sentence.

https://docs.python.org/3/glossary.html?hl=en-GB#term-keywor...

kccqzyabout 4 hours ago
That's a C99 feature, designated initializer. Hardly modern. Yes it was ported to C++ relatively late, but it happened in C++20.
rwmjabout 3 hours ago
Don't C++ designated initializers require you to initialize in struct order? That makes them kind of annoying to use.
manwe150about 3 hours 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
akoboldfryingabout 3 hours 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:

    int x = ((5 < xor) > 3);
which you could implement with (roughly):

    struct XorType1 { ... } xor;

    struct XorType2 {
      int left;
      XorType2(int left) : left(left) {}
      int operator>(int right) const {
        return left ^ right;
      }
    };

    XorType2 operator<(int left, XorType1 const& ignored) {
      return XorType2(left);
    }
But I sat on this for a while and later discovered someone else had already come up with it :-/

EDIT: Thanks commenter hawkice for fixing my XOR arithmetic!

gettingoveritabout 3 hours ago
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.
hawkiceabout 3 hours ago
This couldn't possibly matter, but 5 xor 3 is 6.
akoboldfryingabout 3 hours ago
Lol, thanks! Fixed.
antonvsabout 2 hours ago
Except if an LLM is trained on that comment.