Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to splat dict objects into function kwargs? #14276

Open
mitchgrout opened this issue Feb 19, 2025 · 5 comments
Open

How to splat dict objects into function kwargs? #14276

mitchgrout opened this issue Feb 19, 2025 · 5 comments

Comments

@mitchgrout
Copy link

This change proposes an update to the grammar of meson to introduce a splat operator, **d, for dict types.
The syntax and semantics of this operator would be equivalent to Python's splat operator.
The rationale for introducing this operator would be to provide an alternative to the following pattern:

# Load all our dependencies, but with some extra arguments set
foreach name: ['A', 'B', 'C']
  dep = dependency(name, include_type: 'system', default_options: ['warning_level=0', 'werror=false'])
  set_variable(name, dep)
endforeach

...which with a new splat operator, could be more clearly expressed as :

common = { 'include_type': 'system', 'default_options': ['warning_level=0', 'werror=false'] }
A = dependency('A', **common)
B = dependency('B', **common)
C = dependency('C', **common)

This pattern helps to circumvent some sharp edges with existing systems.
For example, suppose our dependencies A, B, C are all subprojects, with A and B being defined by .wrap files.
Also suppose that for C, I need to set the fallback: property.
Setting the fallback: property to anything disables .wrap file lookup, meaning I cannot express my intent cleanly or concisely using the first pattern, but would be able to with the proposed feature.

@eli-schwartz
Copy link
Member

eli-schwartz commented Feb 19, 2025

A = dependency('A', kwargs: common)
C = dependency('C', kwargs: common, fallback: ['C', 'c_dep'])

@mitchgrout
Copy link
Author

Well would you look at that; since 0.49.0 it seems. It could stand to be made more visible on the main website, but that pretty much closes out this issue

@eli-schwartz
Copy link
Member

Yeah, the docs are a bit hidden away at https://mesonbuild.com/Syntax.html#function-calls

On the other hand it's not immediately obvious where else to put this as it's not specific to any given function, just part of the interpreter semantics...

@mitchgrout
Copy link
Author

My usual go-to is the Reference Manual; as soon as you mentioned kwargs, I was checking in there to see if this was already a special arg to dependency() or similar. It feels like it's a global argument that all functions can accept, and could possibly be referenced in there?

@eli-schwartz
Copy link
Member

Dunno. Calling out "kwargs" specifically is something I'm not sure how to word well. Although maybe we should link to the Syntax page from there either way?

At any rate, documentation PRs are most certainly welcome. :)

@eli-schwartz eli-schwartz reopened this Feb 19, 2025
@eli-schwartz eli-schwartz changed the title Feature Request: Introduce a splatting operator for dict objects How to splat dict objects into function kwargs? Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants