I think this is a feature in Scala. A friend showed me this example:
```
def splitDate(s: String) = s match {
    case s"$day-$month-$year" =>
        s"day: $day, mon: $month, yr: $year"
    case _ => "not a date"
}
```
Note that the example shows two sides of s-strings: the first one as a
pattern, the second one as a format string. (I assume it's irrelevant that
the argument is also called `s`.)

I think the way this works is they have generalized string literal prefixes
so that for certain values of `foo` you can write `foo"stuff in quotes"`. I
assume that's similar to JavaScript tagged template literals, and I believe
the $variable references are recognized by the compiler (so `foo` has no
say in this -- at least that's how it is in JS).

Scala's match/case can be overloaded by classes providing an `unapply()`
method. Scala also has "by-name" parameters (sort of like `&arg` in C++,
where assignment to `arg` actually assigns to a variable provided by the
caller). I hypothesize that all these things are used together to build the
"s-string" concept as a standard library feature (the language reference
has no specific syntax for this).

The only other thing I heard about this is that the pattern matching has
the power of shell globbing, not of regular expressions -- I guess this
means it supports * and ? as wildcards but nothing fancier.

I found it hard to find more info about this using a search engine. (Most
queries lead to descriptions of regular expressions in Scala, or more
general discussion of pattern matching in Scala.) Maybe that's indicative
of some smell... But its existence suggests that this idea has actually
been implemented quite seriously.

--
--Guido van Rossum (python.org/~guido)
Pronouns: he/him (why is my pronoun here?)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/6CQ6I4DS4COV3Q5JTWGA7TWJODBQLJDI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to