-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add a match_class!
macro
#771
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
Comments
Another question is that this would further cement the unification of class and Rust type and would we want that? I'm not opposed to it personally, but it's something to think about. Also, instead of using a |
This worries me a bit. Matching on the internals is something we should do rarely (if ever?) and with great care, so it seems strange to provide a convenient macro to make it easy. I think the example use case in the PR is wrong. The correct implementation of to_int is something like:
(Plus whatever is needed to make the base special case work.) |
What here are you viewing as "internals"? I would expect all the types and methods used here to (eventually) be part of our public API for extension.
Yeah, a better example would have been to something like |
My expectation is that only methods on the type should be able to access the value directly everything else has to go through methods. Otherwise I think you're liable to break subclasses by accessing something you shouldn't. From this perspective nothing should ever access the private data of another class. I haven't thought much about how the shape of our public api, so I'm not sure how extensions are expected to work with values.
Isn't that's still the same. int and float support |
Completely agree! I assume you're referring then to the full pattern matching in the proposal above (i.e. Or by methods did you mean
More generally, I think many implementations of |
Ah, yes, I missed that distinction. In that case I guess this is equivalent to an isinstance check. (The examples in the issue call Of course, this creates another complication: I'm happier with the more limited implementation. |
Summary
Add a
match_class!
macro for implementing behavior that varies based on the built-in type of an object.Detailed Explanation
Doing this currently with
downcast
is a bit awkward. Because it consumes theRc
,you have two options:
if
s.Err(obj)
ofdowncast
to avoid the clone.This avoids a pointless clone at the cost of adding undesirable rightward drift. What we really want is to be able to use a
match
-like syntax that expands to the efficient version above:Drawbacks, Rationale, and Alternatives
The main drawback is that it is another macro. It should be very straightforward to implement and to understand though.
Unresolved Questions
Will allowing a full pattern in each match clause encourage making struct fields public, when they really should not be? An alternative would be to just allow the type name in each match arm.
The text was updated successfully, but these errors were encountered: