Interpreter: add a default() type for keyword arguments#11987
Conversation
|
Could you give an example where this might be useful? |
| override_options : ['b_ndebug=false'], | ||
| ), | ||
| should_fail : true, | ||
| protocol : default(), |
There was a problem hiding this comment.
I would like to see a unit test that verify passing default() to a new kwarg does not trigger FeatureNew/FeatureDeprecated warnings.
One usage I would like (not sure current implementation would work) is to allow using new kwargs without triggering FeatureNew warning. project(..., meson_version: '>=1.0.0')
val = default()
if meson.version().version_compare('>=1.2.0')
val = 'foo'
endif
some_function(..., new_karg_from_1_2: val)This currently would have to be done with an ugly dict: project(..., meson_version: '>=1.0.0')
d = {}
if meson.version().version_compare('>=1.2.0')
d += {'new_karg_from_1_2': 'foo'}
endif
some_function(..., kwargs: d) |
|
Another usage is when the default value for a kwarg is not obvious, sometimes it's platform dependent, and you optionally want to override the default. An example of such case is lib_prefix = default()
if get_option('force-lib-prefix-on-windows')
lib_prefix = 'lib'
endif
library(..., name_prefix: lib_prefix) |
Note that it's for that use-case that I had in mind to name this |
75873c6 to
a7ab2d8
Compare
a7ab2d8 to
5362e1d
Compare
5362e1d to
3b8458f
Compare
|
Looks good but I would also prefer to have a test case checking whether FeatureNew triggers if you're using default(). It's an important corner case, independent of your choice of how to implement it. Also worth testing is:
Overall it's probably better to have a dedicated entry in test cases/common/. |
3b8458f to
c30e8a6
Compare
|
Why is a |
| dependencies: '') | ||
| assert(exe.name() == 'trivialprog') | ||
| test('runtest', exe) # This is a comment | ||
| test('runtest', exe)) # This is a comment |
There was a problem hiding this comment.
I think something went wrong here.
|
I don't like nil because now we're introducing the optional union type, like python's If we're worried about name collisions, maybe |
This gives a way to conditionally override a keyword argument, such as when setting a value specifically for one platform. For example, ```meson library( ..., name_prefix : host_machine.system() == 'windows' ? '' : default(), ) ```
We have the `default()` function now, so we don't need this.
c30e8a6 to
9c520a9
Compare
I don't understand this. Don't you need the same union type for the
Going back to the previous point, The only reason for bringing up |
|
|
||
| name_prefix: | ||
| type: str | array[void] | ||
| type: str |
There was a problem hiding this comment.
Shouldn't this be str | default strictly speaking? This is what I meant with the other comment. I don't see much of a difference in typing annotations between introducing default() and some form of nil / null / none.
My concern is that in Python especially None doesn't have a set semantic meaning. It gets used to mean all kinds of things. I could mean "no argument", "use a default", "I'm avoiding eager binding issues", "I have a tri-state but don't want to use an enum", "I'm returning an error", "I couldn't find a value in a mapping", "this doesn't have to have a value.", "this value is uninitialized", etc. I like
We don't document that every argument can be |
|
It is not required for the
Unless comparing a value to |
comparison of unlike types became a hard error in 0.60, so that's not allowed :). That's why we have the |
Then we can have a |
|
I'm willing to be flexible on the name, but my experience of having a |
|
I agree that it's better to make this a special thing for argument passing (which is by far the most magic part of meson between flattening, disablers and kwargs) that to try to generalize it. |
|
My questions do not concern the semantics, just the naming of what is being introduced. I am not sure whether a It may be just me, but seeing a If there is consensus that |
This type when passed keyword argument will instruct
typed_kwargsto use the default value, but in a way the user can set explicitly. typed_kwargs makes this extremely trivial to implement, but for it to be universally effective we need to have all keyword arguments be handled by typed_kwargs