-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
extend overloading example by mentioning macros #75
Comments
Sorry, which chapter are we talking about? The "Operator overloading" one? If any case, I think this should go in the "macro_rules!" chapter. |
Actually the operator overloading chapter is about something else, sorry for the confusion. I was referring to function overloading. If you are coming from a C++ background and want to know how function overloading translates to Rust, then there are two possibilities:
Maybe there should be a new chapter about function overloading? |
Note that Rust does not have function overloading, hence having a "function overloading" chapter (with that exact name) may be misleading. However, I agree that it would be great to show how to achieve something similar with traits [1] and macros. I think we have two options: (1) We add a "function overloading" chapter, but clearly state that these are tricks to achieve something similar, because Rust doesn't have function overloading. or (2) We stuff these tricks in other chapters (like in the I'm leaning towards the first one. @vks What do you think? [1] This is a little limited at the moment, currently trait methods are specialized based on the concrete type of |
@japaric I prefer option (1). Option (2) would be too hard to find if you are looking for overloading. An example about multiple dispatch would be nice, but it probably should be it's own chapter, as it is fairly advanced. |
I need to think more about this. But as a first idea, your macro example can go with the And the multiple dispatch could be a sub-chapter in the "trait" chapter. (I'm not too sure about this last one) |
Here is an expanded range macro. Playpen link here Here is their macro: // `range!(a..b)` for example
//
// `a..b` => [a,b) (exclusive)
// `a...b` => [a,b] (inclusive)
// `..b` => [0,b)
// `a..` => [a, infinity)
// `a..b, c` => [a,b) in increments of c
macro_rules! range {
(.. $to: expr) => {
std::iter::range(std::num::zero(), $to)
};
(... $to: expr) => {
std::iter::range_inclusive(std::num::zero(), $to)
};
(.. $to: expr, $step: expr) => {
std::iter::range_step(std::num::zero(), $to, $step)
};
(... $to: expr, $step: expr) => {
std::iter::range_step_inclusive(std::num::zero(), $to, $step)
};
($from: expr .., $step: expr) => {
std::iter::count($from, $step)
};
($from: expr .. $to: expr) => {
std::iter::range($from, $to)
};
($from: expr ... $to: expr) => {
std::iter::range_inclusive($from, $to)
};
($from: expr ..) => {
std::iter::count($from, std::num::one())
};
($from: expr .. $to: expr, $step: expr) => {
std::iter::range_step($from, $to, $step)
};
($from: expr ... $to: expr, $step: expr) => {
std::iter::range_step_inclusive($from, $to, $step)
};
} |
I don't feel this is appropriate for the book; using macros for only overloading isn't something that you should do. |
Currently only traits are mentioned in the overloading example. They only work if the functions to be overloaded have the same number of arguments. Using macros it is possible to have more generic overloading. See for example this implementation of a Python-like
range
:The text was updated successfully, but these errors were encountered: