2

Scala does not allow one to say:

def m(f:(numer:Double,denom:Double)=>tan:Double) = {...}

Just like annotating variables with types means a variable at least has some documentation, so would allowing the variables in a function type definition provide some documentation. Since it would be optional, the programmer would decide when to do it. But the above is definitely more informative than:

def m(f:(Double,Double)=>Double) = {...}

Would this added flexibility break the language syntax?

1
  • 1
    Once you go this route, you pretty much have to support named arguments as well. That is, you should be able to say f(number=..., denom=...) or f(denom=..., number=...). You can leave it out, of course, but it's inconsistent. Commented Dec 5, 2013 at 15:28

2 Answers 2

6

The workaround could be found in using type aliases.

type Numer = Double
type Denom = Double
type Tan   = Double
def m(f:(Numer,Denom)=>Tan) = {...}

Having syntax in your way brings questions and ambiguity -- e.g. will compiler check that target function will have the very same variables names or not? (think about user who will stumble that feature you're proposing)

Sign up to request clarification or add additional context in comments.

2 Comments

But Scala rightly touts no need for ; , and this adds 3 lines. There would be no need for the implementing function to use the same names. The complexity of this feature is very tiny indeed relative to overall complexity of the language. I know, one straw can break the camels back...
The main problem with this solution is replaces the explicit variable type with another type that provides an indication of what its function is. The two parts next to each other is more informative.
0

A middle-of-the road workaround, similar to om-nom-nom's, might be to have a Fraction class, which is composed of a numerator and denominator along with a more descriptive name for f. Something like:

case class Fraction(numerator: Double, denominator: Double) { ... }

...

def doSomething(getTangent: (Fraction) => Double) = { ... }

The weakness here is that there's nothing stopping someone from passing a function of type (Fraction) => Double which actually returns a sine, cosine, etc. So you may consider, instead of getTangent, using the name trigFunc or something like that.

As for your original suggestion, as has been mentioned already, I think you wouldn't want to enforce that the function implementation uses the same parameter names as in the type description, therefor parameter names in the type description are effectively just comments, not anything to be enforced by the compiler. So a scaladoc comment might serve the same purpose.

1 Comment

You are right the primary purpose is documentation. Scaladoc has a bit more activation energy, so often it does not get done because it seems unnecessary at the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.