-
Notifications
You must be signed in to change notification settings - Fork 784
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
Create macro for creating kwargs dict? #1463
Comments
Absolutely! I've thought about this before and I think we can do one better and support python collections a-la py_list!(py, [a, b, c]);
py_tuple!(py, (a, b, c));
py_dict!(py, {a => 1, b => 2, c => 3}); // or {a: 1, b: 2, c: 3} for if that's technically possible, maplit says it's not
py_set!(py, {a, b, c});
py_frozenset!(py, {a, b, c}); I think these have more general use than a Also when we add functionality to use |
I wonder if we can take the list![a, b, c];
tuple!(a, b, c);
dict!{a => 1, b => 2, c => 3};
set!{a, b, c};
frozenset!{a, b, c}; so that Python::with_gil(|py| {
let my_list = list![1, 2, 3]
}); would expand to something like Python::with_gil(|py| {
let my_list = PyList::empty().unwrap();
my_list.append(py, 1).unwrap();
my_list.append(py, 2).unwrap();
my_list.append(py, 3).unwrap();
}); I think it would look more familiar to Python users. I'm not sure though that being implicit here is a good idea. Also it relies heavily on the convention to name the |
And it doesn't even work (with macro_rules macros) since you can't use outer identifiers in the expansion without passing them in. That shows that Rust doesn't want us to do this in the first place :) |
Ah, right, thanks. Never mind then :) |
Reminder for myself: I was thinking it might be nice if we can also use spread syntax in these macros. e.g. let left = py_list!(py, [a, b]);
let right = py_list!(py, [c, d]);
let joined = py_list!(py, [*left, *right]); |
I'm not a fan of this (nor
I do like this syntax. |
While calling methods with only positional arguments of different types is pretty easy (with tuples of
IntoPy
), for kwargs it gets complicated (see #1462).Would it be useful to have a macro that can be used like so:
The text was updated successfully, but these errors were encountered: