> 24 Nov. 2017 08:33 Chris Lattner via swift-evolution
> <[email protected]> wrote:
>
> 2) I’d like to explore the idea of making // syntax be *patterns* instead of
> simply literals. As a pattern, it should be possible to bind submatches
> directly into variable declarations, eliminating the need to count parens in
> matches or other gross things. Here is strawman syntax with a dumb example:
>
> if case /([a-zA-Z]+: let firstName) ([a-zA-Z]+: let lastName)/ =
> getSomeString() {
> print(firstName, lastName)
> }
Erlang has something very similar for binaries, where constructing and matching
a binary is part of the syntax.
For example:
C = <<A:1, B:63>>
constructs a 64-bit binary C where the first bit comes from the integer
variable A, and the rest from the integer variable B. In Erlang, this syntax is
exactly the same when matching and constructing, so the corresponding syntax
for matching is:
<<A:1, B:63>> = C
where the 64-bit binary C is matched so that the first bit is put in the
integer variable A and the rest is put in the integer variable B. If we don't
want to match a variable to an integer but keep it as a binary, we can just
mark the variable matching as a binary:
<<A:8, B/binary>> = C
which means that A will still be an integer (but in this case 8 bits wide) and
B will be a binary.
Or if we want to do different things based on the first bit:
case C of
<<0:1, B:63>> ->
B;
<<1:1, B:63>> ->
B + 10
end
Making this kind of powerful syntax work for regular expressions would be very
nice in Swift.
(And I would like it for binaries as well)
/Magnus
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution