Why does Scala need parameterless in addition to zero-parameter methods?

前端 未结 5 1226
猫巷女王i
猫巷女王i 2020-12-04 17:58

I understand the difference between zero-parameter and parameterless methods, but what I don\'t really understand is the language design choice that made parameterless metho

5条回答
  •  悲&欢浪女
    2020-12-04 18:35

    One nice thing about an issue coming up periodically on the ML is that there are periodic answers.

    Who can resist a thread called "What is wrong with us?"

    https://groups.google.com/forum/#!topic/scala-debate/h2Rej7LlB2A

    From: martin odersky Date: Fri, Mar 2, 2012 at 12:13 PM Subject: Re: [scala-debate] what is wrong with us...

    What some people think is "wrong with us" is that we are trying bend over backwards to make Java idioms work smoothly in Scala. The principaled thing would have been to say def length() and def length are different, and, sorry, String is a Java class so you have to write s.length(), not s.length. We work really hard to paper over it by admitting automatic conversions from s.length to s.length(). That's problematic as it is. Generalizing that so that the two are identified in the type system would be a sure way to doom. How then do you disambiguate:

    type Action = () => () def foo: Action

    Is then foo of type Action or ()? What about foo()?

    Martin

    My favorite bit of paulp fiction from that thread:

    On Fri, Mar 2, 2012 at 10:15 AM, Rex Kerr  wrote:
    
    >This would leave you unable to distinguish between the two with 
    >structural types, but how often is the case when you desperately 
    >want to distinguish the two compared to the case where distinguishing 
    >between the two is a hassle?
    
    
    /** Note to maintenance programmer: It is important that this method be
     *  callable by classes which have a 'def foo(): Int' but not by classes which
     *  merely have a 'def foo: Int'.  The correctness of this application depends
     *  on maintaining this distinction.
     *  
     *  Additional note to maintenance programmer: I have moved to zambia.
     *  There is no forwarding address.  You will never find me.
     */
    def actOnFoo(...)
    

    So the underlying motivation for the feature is to generate this sort of ML thread.

    One more bit of googlology:

    On Thu, Apr 1, 2010 at 8:04 PM, Rex Kerr <[hidden email]> wrote: On Thu, Apr 1, 2010 at 1:00 PM, richard emberson <[hidden email]> wrote:

    I assume "def getName: String" is the same as "def getName(): String"

    No, actually, they are not. Even though they both call a method without parameters, one is a "method with zero parameter lists" while the other is a "method with one empty parameter list". If you want to be even more perplexed, try def getName()(): String (and create a class with that signature)!

    Scala represents parameters as a list of lists, not just a list, and

    List() != List(List())

    It's kind of a quirky annoyance, especially since there are so few distinctions between the two otherwise, and since both can be automatically turned into the function signature () => String.

    True. In fact, any conflation between parameterless methods and methods with empty parameter lists is entirely due to Java interop. They should be different but then dealing with Java methods would be just too painful. Can you imagine having to write str.length() each time you take the length of a string?

    Cheers

提交回复
热议问题