Error E513: Parenthesis are required to call method in Silver 10.0.0.2289

,

class Main
{
func main()
{
let f = sayHi // this does not compile: “error E513: Parenthesis are required to call method sayHi()”
foo(doit: f)

	foo(doit: sayHi) // this also does not compile error: E558: Parenthesis required for call in parameter 1 of Main!.sayHi()

	let f2 : FuncType = sayHi // this workaround compiles
	foo(doit: f2)
}

func sayHi()
{
	print("hi")
}

typealias FuncType = ()->Void

func foo(doit: FuncType)
{
	doit()
}

}

Namespace.Main().main() // change Namespace to yours

1 Like

Yes, that seems expected? Method calls need () in Swift, same as C, C# and most other related languages.

In C++ and C#, it’s possible to pass functions as parameters without having to create a temporary variable. So this in not really as expected. The code I posted compiles and runs correctly in two different “online swift compiler in a webpage” sites I tried (if you tweak the last line).

Ah, got you. I misread, yers that should work. although I’m not sure about type inference, as that could lead for some subtle errors when forgetting () but actually intending to call the function (ie let f = sayHi vs let f = sayHi() Not sure if that’s desirable. But I’ll log this for investigation, And passing it as parameter definitely should work.

Thanks, logged as bugs://80300

It would be great if I could do this with one line of code:

foo(doit: sayHi)

where “sayHi” is a member function. I used to do this in C++ and C# all the time. I believe it’s totally type safe too. This is mainly the point of my post.

Yes, I agree with you about “let f = sayHi”. I don’t really plan to do that. I just included that to help you debug the issue.

Thanks for taking a look at this!

Yeah, that one needs to be fixed, no question. For the other, I’ll see what Apple Swift does, and then we’ll see…

bugs://80300 got closed with status fixed.