[swift-users] Function to unsafe pointer and back

Andrew Trick atrick at apple.com
Thu Sep 1 15:48:45 CDT 2016

> On Sep 1, 2016, at 12:37 PM, Lou Zell via swift-users <swift-users at swift.org> wrote:
> As to your real question, what’s your high-level goal?  Swift doesn’t really do pointers to functions [1] but it does provide lots of other excellent ‘treat code as data’ features.  If you can explain more about your goal, perhaps we can direct you to a better path.
>> Curiosity got the better of me on this one - there's no higher level goal other than improved understanding.  I was playing around with function currying in the repl and that's what lead me to those experiments.  
> The article, and the preceding one in the series, has plenty for me to work with.  Thank you!

That’s an awesome article, but I don’t think you need to understand any of it! I’m not an expert in this either, but here’s what I can see from your code...

It looks like you’re trying to capture the address of a function that was JIT’d for the purpose of evaluating a statement in the repl. That address might not be valid in the next statement.

Also, in the first case the UnsafePointer points to local memory that holds the function value. In the second case, you’re trying to load a function value from the address of the function body. So a level of indirection is missing.

You can’t take an address to a function in Swift. You can assign a variable to the function, as you did in the first case:

var x = doNothing

And view the address of that variable as an argument, only for the duration of the call, as you did in the first case:



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160901/fae5568e/attachment.html>

More information about the swift-users mailing list