Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cannot call value of non-function type" error when attempting to call the max(_:_:) function

Tags:

generics

swift

I'm trying to call the max function: max(x: T, y: T). However I keep getting the following error when I type max(2,3):

error: cannot call value of non-function type Int var a = max(2, 3)

I am a beginner, and I have never encountered a function signature that uses a type "T". SO threads relating to using the max function call it in the manner I am (like max(2,3) ) so I am not sure where I am going wrong.

I am looking for an explanation on the "T" and how to call functions that support generic types and how to make the max function return 3 when comparing integers 2 and 3.

like image 264
Govind Rai Avatar asked Aug 06 '16 21:08

Govind Rai


2 Answers

The problem (as you've confirmed in the comments) is that you have defined a variable named max, causing a naming conflict with the function max(_:_:).

The solution therefore is to either specify the Swift module namespace (as George suggested) in order to disambiguate the fact that you're referring to the max(_:_:) function:

Swift.max(2, 3)

Or, preferably, you should consider renaming your variable. I strongly suspect that there's a more descriptive name you could give it (remember, the Swift API Design Guidelines favours clarity over brevity).

like image 108
Hamish Avatar answered Sep 21 '22 15:09

Hamish


Are you calling max within extension Int?

Try Swift.max(2, 3).

like image 43
George Avatar answered Sep 23 '22 15:09

George