Is there any fundamental limitations that stops Scala from implementing pattern matching over functions?

后端 未结 4 1827
不思量自难忘°
不思量自难忘° 2021-02-18 17:54

In languages like SML, Erlang and in buch of others we may define functions like this:

fun reverse [] = []
|   reverse x :: xs  = reverse xs @ [x];
4条回答
  •  臣服心动
    2021-02-18 18:07

    I don't know SML or Erlang, but I know Haskell. It is a language without method overloading. Method overloading combined with such pattern matching could lead to ambiguities. Imagine following code:

    def f(x: String) = "String "+x
    def f(x: List[_]) = "List "+x
    

    What should it mean? It can mean method overloading, i.e. the method is determined in compile time. It can also mean pattern matching. There would be just a f(x: AnyRef) method that would do the matching.

    Scala also has named parameters, which would be probably also broken.

    I don't think that Scala is able to offer more simple syntax than you have shown in general. A simpler syntax may IMHO work in some special cases only.

提交回复
热议问题