Swift area - self-binding (means "I as a wrapping function")

Take this basic function:

func sampleFunction () { print( self ) } 

Here, self refers to an instance of a class that converts it, and not how it may make more sense - a function that converts it.

How to get a link to an envelope function, not a shell class instance?

Ideally, I want to do something like this:

 func sampleFunction ( value: Int ) { print( selfAsEnvelopingSampleFunction, value ) } 
+5
source share
2 answers

If you want to print a function name, you can use __FUNCTION__

 print(__FUNCTION__) 

in your print statement, else just use sampleFunction as an argument.

+4
source

Instance methods are displayed in quick mode. That way you can get the function as a reference.

 struct User { func sampleFunc(name: String) { let f = User.sampleFunc(self) print(f, name) } } 

Here the value of f is the function itself

+1
source

Source: https://habr.com/ru/post/1237408/


All Articles