

C experiments in Swift - agp2572
https://github.com/ankurp/unsafe-swift

======
tambourine_man
Check this talk, "Swift and C" by Mike Ash:

[http://vimeo.com/107707576](http://vimeo.com/107707576)

~~~
ankurpatel
Yes that talk is also good. You can see his playground here
[https://mikeash.com/tmp/%f0%9f%90%8d.zip](https://mikeash.com/tmp/%f0%9f%90%8d.zip)

~~~
genieyclo
Thanks, I dig the emoji+Swift love.

------
stonewhite
Everything is great until the flamebait at end.

"It is possible to do it all in Swift making it a versatile language unlike
Java or Scala"

Anybody cares to elaborate on that?

~~~
ankurpatel
Having done Java programming I have not been able to write code where I can
directly allocate memory in the heap and iterate over it like in C. Correct me
if I am wrong with an example.

~~~
virmundi
I don't think this addresses your question directly, but there is always
Unsafe. You can do a lot of direct memory manipulations through it.
[http://highlyscalable.wordpress.com/2012/02/02/direct-
memory...](http://highlyscalable.wordpress.com/2012/02/02/direct-memory-
access-in-java/)

~~~
ankurpatel
Good article but trying to write code like in the examples seems very
cumbersome compared to writing it in Swift. The Java language syntax is partly
to blame.

------
asveikau
I wonder how longjmp() interacts with ARC. My guess is poorly.

~~~
bitserf
You can manually retain / release using Unmanaged, but that seems like a lot
of work when you can mix C/C++ and Swift in the same target.

~~~
ankurpatel
Right if you are using longjmp you probably want to manage memory youself.

~~~
mikeash
You also need to use volatile if you expect writes to variables to persist
when calling longjmp(), because any value the compiler decides to stash in a
register will be overwritten. Since Swift lacks volatile, you won't be able to
make that work properly. It's fun to play with though.

------
panzi
I guess using setjmp/longjmp to implement something like try-catch in C is
something everyone does when they discover this the first time. I did. But I
was aware that it isn't a good idea, I just wanted to try it. It was some time
between 13 and 11 years ago. I guess other computer science students did the
same since the 80ies?

~~~
userbinator
The most interesting use of setjmp/longjmp I've seen is in QEMU, which uses
them to implement coroutines.

------
sirseal
"Writing try-catch block using setjmp and longjmp" Great! Now I can have fun
with some bad, old programming ideas!

