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

Switch Value to use the inherit/cast model #703

Merged
merged 4 commits into from
Aug 5, 2021
Merged

Switch Value to use the inherit/cast model #703

merged 4 commits into from
Aug 5, 2021

Conversation

jonmeow
Copy link
Contributor

@jonmeow jonmeow commented Aug 4, 2021

Minor note, I do cast<Type>(*val).Call() for consistency with the Pattern change, but I think cast<Type>(val)->Call() would work the same. I wanted to note this if it wasn't a deliberate choice, before we dig in on this route.

@google-cla google-cla bot added the cla: yes PR meets CLA requirements according to bot. label Aug 4, 2021
@jonmeow jonmeow requested a review from geoffromer August 4, 2021 20:55
@jonmeow jonmeow marked this pull request as ready for review August 4, 2021 21:08
@jonmeow jonmeow requested a review from a team as a code owner August 4, 2021 21:08
Copy link
Contributor

@geoffromer geoffromer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note, I do cast<Type>(*val).Call() for consistency with the Pattern change, but I think cast<Type>(val)->Call() would work the same. I wanted to note this if it wasn't a deliberate choice, before we dig in on this route.

I'd sort of prefer to avoid cast<Type>(val) when val is a pointer because it feels a little too DWIM-ish the cast says Type but the result is actually Type*. I'd be fine with cast<Type*>(val), though, assuming that works.

Comment on lines 281 to 286
CHECK(false)
<< "arity mismatch in tuple pattern match:\n pattern: "
<< p_tup << "\n value: " << v_tup;
FATAL_RUNTIME_ERROR(line_num)
<< "arity mismatch in tuple pattern match";
<< "arity mismatch in tuple pattern match:\n pattern: "
<< p_tup << "\n value: " << v_tup;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing you meant to delete one of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry about that, the CHECK was for debugging

@@ -759,7 +762,8 @@ void StepPattern() {
if (act->pos == 0) {
if (tuple.Fields().empty()) {
frame->todo.Pop(1);
frame->todo.Push(Action::MakeValAction(Value::MakeTupleValue({})));
frame->todo.Push(Action::MakeValAction(
global_arena->New<TupleValue>(std::vector<TupleElement>())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should give TupleValue a default constructor that's equivalent to constructing from an empty vector? It's a little inelegant, but I expect creating empty tuples (e.g. the unit type) to be a moderately common pattern, so it'd be nice to make it less verbose.

(It's so frustrating that C++ doesn't give us better options here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll send a separate PR for this, I think it'll be easier to review that way.

auto Name() const -> const std::optional<std::string>& { return name; }
auto Type() const -> const Value* { return type; }

private:
// nullopt represents the `_` placeholder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should probably go on the accessor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jonmeow jonmeow merged commit 113430a into carbon-language:trunk Aug 5, 2021
@jonmeow jonmeow deleted the val-inherit branch August 5, 2021 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes PR meets CLA requirements according to bot.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants