-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Value patterns as function parameters #578
Comments
I think part of this question is either inextricably linked to #528, or at least depends on #528, in that if #528 decides we want the same pattern matching syntax everywhere, then part (2) of the proposed change would apply to all pattern-matching contexts and cannot be decided for function parameters in isolation. In a comment on 528 I suggested that we should not allow runtime-refutable patterns in parameters, which might also address this issue, if If our decision for part (1) is to make the above declaration of |
Another possibility is we use singleton types to match a single value. We are talking about all types and all values that can be written as a literal having singleton types anyway. We could define a |
Another option, from @geoffromer
|
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This is related to the question in #528 . In particular, both would be addressed by an introducer to match a value, as suggested in this thread on #syntax starting 2022-09-16. |
The status quo, as of #2188, is that a declaration such as:
is ill-formed, because |
For this use case, we can use something like this:
|
There is another example from https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#parameterized-interfaces // Doesn't work, no support for `singleton_type_of`
fn PeekAtTopOfStackParameterized
[T:! type, StackType:! StackParameterized(T)]
(s: StackType*, _:! singleton_type_of(T)) -> T { ... } This corresponds to the C++ code: // Called `PeekAtTopOfStackParameterized<MyType>(&MyStack)`
template<typename T, StackParameterized<T> StackType>
auto PeekAtTopOfStackParameterized(StackType* s) -> T { ... } We discussed a couple possible workarounds: fn PeekAtTopOfStackParameterized[StackType:! type]
(s: StackType*, T:! type where StackType is StackParameterized(.Self)) -> T {
let ActualStackType:! StackParameterized(T) = StackType;
let actual_s: ActualStackType* = s;
...
} fn PeekAtTopOfStackParameterizedImpl
(T:! type, StackType:! StackParameterized(T), s: StackType*) -> T {
...
}
fn PeekAtTopOfStackParameterized[StackType:! type]
(s: StackType*, T:! type where StackType is StackParameterized(T)) -> T {
return PeekAtTopOfStackParameterizedImpl(T, StackType, s);
} We rejected this approach in https://github.com/carbon-language/carbon-lang/blob/trunk/proposals/p2188.md#type-pattern-matching : fn PeekAtTopOfStackParameterized
(T:! type, s: (StackType:! StackParameterized(T))*) -> T { ...} Two ways we could in the future support this use case would be: fn PeekAtTopOfStackParameterized
(T:! type, s: (some StackParameterized(T))*) -> T { ...} // Called `PeekAtTopOfStackParameterized(MyType)(&MyStack)`:
fn PeekAtTopOfStackParameterized(T:! type) =>
lambda [StackType:! StackParameterized(T)](s: StackType*) -> T {
...
} |
Right now there is a gotcha in our current strategy for using pattern matching for function parameters:
may only be called by
F(Int)
, notF(1)
orF(2)
. This is a significant and confusing divergence from C++. There was some discussion of this in the #syntax discord channel.I'd like to propose to change things in two ways:
F
is illegal or it means thatF
takes an argument of typeInt
, like it would in C++.We might forbid value patterns in function declarations entirely, but there are some rare situations where they are useful. For example, it is useful for using a later type parameter in an earlier parameter's type, as in:
I propose for 2 that we mark value patterns with an
==
prefix, as in:The question of whether this would also be used in
match
statements as in:is the subject of #528 .
The text was updated successfully, but these errors were encountered: