In the TypeScript below both functions are the same, except that I'm trying to explicitly declare the return type in demoTwo. The return type is a function which itself takes a function as input. My question is why do I have to give the parameter name represented by whyThis, given that it will never be used? The code will not compile without something in that position.
function demoOne() {
return function(input: () => string) : void {
var result = input();
console.log("Foo:",result);
}
}
function demoTwo(): (whyThis:() => string) => void {
return function(input: () => string) : void {
var result = input();
console.log("Bar:",result);
}
}
var sampleInput = () => "wibble";
demoOne()(sampleInput);
demoTwo()(sampleInput);
To be clear what I'm asking here's the equivalent code in Scala:
object Program {
def demoTwo(): (() => String) => Unit = {
def tmp(input: () => String): Unit = {
val result = input()
println("Bar: " + result)
}
return tmp
}
def main(args: Array[String]): Unit = {
val sampleInput = () => "wibble"
demoTwo()(sampleInput)
}
}
If we set the declarations of demoTwo side by side we have:
function demoTwo(): (whyThis:() => string) => void { //TS
def demoTwo(): (() => String) => Unit = { //Scala
The only major difference is that TS requires something at the whyThis position and Scala does not. Why should this be the case?