Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a global function which has the same name as a member function

Tags:

swift

The Array type in Swift has a member function called sort, with its signature being sort(isOrderedBefore: (T, T) -> Bool). This function differs from the global version of sort, which has the signature sort(array: T[], pred: (T, T) -> Bool).

If one extends an Array (see Why does the same method fail when inside an Array extension in Swift?), calling sort from inside the scope of the Array extension will naturally result in the local version being used.

Is it possible to explicitly call a function from an outer scope, or specifically from the global scope, even if its name coincides with that of a function from an inner scope?

This would be similar to the C++ scoping resolution operator, ::

like image 652
Cezar Avatar asked Jun 05 '14 02:06

Cezar


People also ask

Can different classes have member functions with the same name?

Yes, but only if the two classes have the same name.

How do you make a global function in Swift?

We will be using the Playground. Here you can see section is a global variable we have defined, inside a class but outside a function. We can use an access modifier prefixed with the global variable as per the need. You can also define a global variables as static by prefixing static keyword.

How do you define a global function in CPP?

What you are calling global function is usually called a free function and they are A Good Thing. You would define it just like a class' member function, but outside of that class' scope. double squared(double x); and put the implementation (first example) into the *.

What is a global function?

Global functions allow you to perform processing at a specific resolution and extent. By default, global functions process rasters at the source resolution and full extent. This means that applying a global function may take some time, depending on the size of the outputs.


1 Answers

Chris Lattner suggests qualifying the name of the global function with Swift's default namespace, Swift. So you should be able to access the global version using: Swift.sort.

like image 92
Yawar Avatar answered Sep 22 '22 01:09

Yawar