two type parameters with the same name

前端 未结 4 767
野趣味
野趣味 2021-01-02 23:44

I am wondering why the two type parameters (named \"A\") with the same name (\"A\") is allowed as per the example below. I know this is a POOR naming of type parameters, do

4条回答
  •  一向
    一向 (楼主)
    2021-01-03 00:17

    Well I belive at scala we use same rule as in Java basically we are looking for smallest available scope in Java:

    class Foo{
       T instance;
    
       void  T getInstance(){
           return instance
        }
    }
    

    Will produce a compilation error since type T declared in generic method getInstance is not the same as parameter type of class Foo. In case of Scala I believe then you write

    def checkString[A]
    

    You telling compiler that function behavior will vary upon provided type but it has no connection with parameter class of outer class. Unfortunately I cannot find correct place is Scala spec right now.

提交回复
热议问题