The problem is that you are expanding Int to add a variable called abs , which is also the name of the function being called.
When you try to call the abs() function on Int , it sees the created abs variable, and it gets confused because it thinks you are trying to return this variable and donβt understand why you are sending the parameter to it.
If you rename the variable to absoluteValue or anything else, it should work.
let value = -13 abs(value) extension Int { var absoluteValue:Int { return abs(self) } } value.abs
Update:. As others have argued, you can also resolve the issue of the uniqueness of using abs by explicitly calling a function within Swift . This should work as well as the above solution.
let value = -13 abs(value) extension Int { var abs:Int { return Swift.abs(self) } } value.abs
Although I would personally rename my new function to absoluteValue , as in the first example, so that it is clear that you are not calling Swift.abs() when using your abs variable.
source share