-
Notifications
You must be signed in to change notification settings - Fork 21
Open
Labels
Milestone
Description
Scala currently has a method StringLike#r for compiling strings to regexes.
Now that Scala has string interpolation and macros I think we can do even better with a r method on StringContext. Here is why:
-
Escaping gets simpler (before:
"""\d+""".r, after:r"\d+") -
Parameterization gets simpler (before:
("foo"+Pattern.escape(bar)).r, after:r"foo$bar") -
Regexes can be syntax-checked at compile-time
-
Pattern matching against regexes gets simpler (before:
val re = """\d+(.*)\d+""".r; … casere\(x\) => x, after:case r"\d+$x\d+" => x)
I have a proof of concept implementation available at https://github.com/qerub/scala-regex-stringcontext and can make a patch for Scala proper if people like this idea.