Scala's equivalent to Haskell's as-patterns in pattern matching?

86 Views Asked by At

I have pattern matching in Scala with a long case of which I use a large part. Can I name the part of the case so I don't have to rewrite the whole expression?

Example:

x match {
    case (("def", symPos) :: defRest, defPos) :: rest =>
        val (defs, others) = split(rest)
        ((("def", symPos) :: defRest, defPos) :: defs, others)
    // …
}

The pattern (("def", symPos) :: defRest, defPos) is just to detect a certain structure in the data; I don't need to manipulate its internals. If I could refer to the whole thing, the code would be shorter and clearer.

In Haskell, I could write it this way:

case x of
    def@(("def", _) : _, _) : rest ->
        let (defs, others) = split rest
        in (def : defs, others)
    -- …

Here, I name the greater pattern def, which allows me to omit the names of its internals and use just the name to put it to a list. Can I do something like that in Scala? I didn't find anything.

0

There are 0 best solutions below