Hacker News new | past | comments | ask | show | jobs | submit login
C experiments in Swift (github.com/ankurp)
57 points by agp2572 on Nov 2, 2014 | hide | past | favorite | 18 comments



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

http://vimeo.com/107707576


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


Thanks, I dig the emoji+Swift love.


Thanks for pointing it out.


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


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.


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


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.


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?


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.


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...


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.


new byte[]?


Why would you want to do that in Java?


> where I can directly allocate memory in the heap

Doesn't the "new" keyword always heap-allocate? I guess modulo optimizations like "this is a value-class that won't escape the scope of the method: "in-line" it" (if that is a technique that is in use/possible). Also, it seems reasonable that an array of primitive values would actually be represented as a contiguous piece of memory in the heap.

Sorry if that wasn't what you asked about.


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?


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


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: