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.
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).
Are you calling max within extension Int?
Try Swift.max(2, 3).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With