Skip to content

Add r method to StringContext (for regexes) #7496

Open
@scabug

Description

@scabug

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:

  1. Escaping gets simpler (before: """\d+""".r, after: r"\d+")

  2. Parameterization gets simpler (before: ("foo"+Pattern.escape(bar)).r, after: r"foo$bar")

  3. Regexes can be syntax-checked at compile-time

  4. Pattern matching against regexes gets simpler (before: val re = """\d+(.*)\d+""".r; … case re\(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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions