Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Class have a variable name same as function name

I have the following class in Swift 4:

class AClass {
    var add: (Int) -> Int {
        return {
            num in
            return num + 1
        }
    }

    func add(_ num: Int) -> Int {
        return num + 20
    }
}

Note that the variable and function have the same name 'add'.

Now in some other place I have this code:

let a = AClass()
print(a.add(1))

I have run this code, and the result is 2 (which means the variable's block is called).

So here are the questions:

  1. Does the compiler always get the variable rather than call the function?

  2. Is there any way to call the function?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In Swift, instance methods are curried functions. Which means your add method is actually

static func add(_ self: AClass) -> (Int) -> Int

This is why the property gets chosen over the function. Calling AClass.add(a)(1) yelds the expected result, the function gets called.

Now, why does the compiler allows this name clashing if the first place? I'm not sure, but I assume it has to do with the fully qualified name of those two entities. The property is simply add, while the function is add(_:).

over 4 years ago · Santiago Trujillo Report

0

While both the property and method share the same base name add, they have different full names. The method's full name is add(_:) due to the fact that it has a parameter (with no argument label), and the property's full name is just add. The fact that their full names differ is what allows them to overload each other.

If the method had no parameters, then the compiler would not have allowed the overload, as their full names are now both add and therefore conflict:

class AClass {
  var add: () -> Int {
    return {
      return 1
    }
  }

  func add() -> Int { // error: Invalid redeclaration of 'add()'
    return 2
  }
}

Does the compiler always get the variable rather than call the function?

Assuming they have the same function type (such as in your example), then yes. There is an overload ranking rule that favours variables over functions:

    // If the members agree on instance-ness, a property is better than a
    // method (because a method is usually immediately invoked).
    if (!decl1->isInstanceMember() && decl2->isInstanceMember())
      score1 += weight;
    else if (!decl2->isInstanceMember() && decl1->isInstanceMember())
      score2 += weight;
    else if (isa<VarDecl>(decl1) && isa<FuncDecl>(decl2))
      score1 += weight;
    else if (isa<VarDecl>(decl2) && isa<FuncDecl>(decl1))
      score2 += weight;

lib/Sema/CSRanking.cpp

In order to call the method, you can use refer to it by its full name, for example:

let a = AClass()
print(a.add(_:)(1)) // 21
over 4 years ago · Santiago Trujillo Report

0

change the definition of your method to this:

func add(num: Int) -> Int {
    return num + 20
}

notice, I removed the _ this will force you to call the method this way add(num:) thus, you will be able to distinguish between a method call and a variable initialization.

for your question 1.) what you actually claim to be a variable is actually a closure.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!