Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-7496

Add r method to StringContext (for regexes)

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Library (Misc)
    • Labels:
      None

      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:

      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.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              qerub Christoffer Sawicki
            • Votes:
              10 Vote for this issue
              Watchers:
              18 Start watching this issue

              Dates

              • Created:
                Updated: