5 ms·
What is a "Swift-only" integer? Swift integers are just integers. They interoperate with C just fine. Interacting with C APIs in Swift is pretty much as conven
by mikeash 11y ago
What is a "Swift-only" integer? Swift integers are just integers. They interoperate with C just fine.
Interacting with C APIs in Swift is pretty much as convenient as using them from C or Objective-C. Which is to say, it's kind of inconvenient compared to using a good Swift API, but you don't gain a benefit from using C or Objective-C instead.
The major exceptions to this are the inability to take a pointer to something before it's initialized (for example to use a local variable for an out-parameter) and the inability to declare arrays on the stack. The former just requires some "= 0" or similar, and the latter can be worked around without much difficulty using a Swift Array or just using malloc and free, at the expense of a bit of performance.
There is also a problem where Swift translates fixed-length arrays to tuples (because Swift has no concept of fixed-length arrays) which can be inconvenient, but this rarely comes up.
Maybe you're just not familiar with the facilities Swift provides for interacting with C APIs and are doing things in a more difficult way than is necessary.
It's absolutely true for C++ interfaces, of course, but Swift doesn't even try and never advertises C++ interop. For that, you have no choice but to use C++ or Objective-C++. But for plain C APIs, Swift does a great job.
- hellofunk 11y agoWhat about stuff like this, these weird castings that pop up in Swift that you don't worry about in Objective-C when dealing with C APIs: glBindBuffer(GLenum(GL_ARRAY_BUFFER), bufferIds[1]) glBufferData(GLenum(GL_ARRAY_BUFFER), sizeof(Float) * tcSize, tc, GLenum(GL_STATIC_DRAW)) That just looks silly to me.
- potatolicious 11y agoSwift enums are considerably different from C/Obj-C enums so the cast is unavoidable. Specifically unlike C enums, Swift enums can be backed by non-integer types like Strings. For numeric types that have a clear equivalence in C-land, Swift is seamless.
- comex 11y agoActually, GLenum is not a C enum in the first place; it's just an integer typedef.
- mikeash 11y agoThe lack of implicit integer conversions does dirty up the code a bit when using C APIs built around them, but I don't find it to be a particularly big deal. Regarding the post in your other comment, none of that really has anything to do with C interop. C doesn't even have generics, so the whole thrust of the article (that it's hard to build a generic function that works on all integer types) doesn't apply.
- cballard 11y agoThis appears to be a peculiarity of the OpenGL headers - GL_ARRAY_BUFFER is just a #define: #define GL_ARRAY_BUFFER 0x8892 Instead of a proper constant. Therefore, Swift interprets it as an integer, which is fairly reasonable. GLEnum is: typedef uint32_t GLenum; Which is not compatible with Int in Swift, thus the "cast". If GL_ARRAY_BUFFER was: extern GLenum GL_ARRAY_BUFFER; It would work as expected.
- hellofunk 11y agoI've read a number of articles like this one regarding the Integer protocol in Swift and the many different int types: http://blog.krzyzanowskim.com/2015/03/01/swift_madness_of_generic_integer/ http://blog.krzyzanowskim.com/2015/03/01/swift_madness_of_ge... I guess everyone's mileage varies, but I find it a bit strange.
- eridius 11y agoThe only thing strange is that the author of the post seems to be trying to make Swift's integer handling sound unnecessarily complex. > There are 11 Integer types that can be considered as Integers Although he's including Bit which doesn't really count (it conforms to the appropriate protocols but from the doc comment you can see it's actually just intended as the index type for CollectionOfOne). But having a bunch of different integer types shouldn't be surprising. Every integer type Swift has corresponds to an integer type in C, e.g. Int16 is int16_t, UInt64, is uint64_t, Int is long, etc. As for the protocols, so what? It's a bad thing that most of the functionality of integers can be represented using a bunch of protocols? Clearly not. All the author is trying to do is make things sound complex. > My goal here is to create generic function that will handle any integer value and make some operation on it. In this case I want to build function that get bytes and return integer value build with the given bytes array of bytes So he wants to have a function that builds integer types all different sizes given an array of bytes, and wants to do it generically for any integer? Ok, take a pointer to your bytes, cast it to a pointer to integers of the right size, and you're done. Oh wait.. he wants to do it without "raw memory", whatever that means? That's stupid. The rest of the article is nothing but a complicated exercise in trying to do things as hard as possible. His entire function can be written in pure Swift (without NSData) as func integerWithBytes<T: IntegerType>(bytes: [UInt8]) -> T? { guard bytes.count >= sizeof(T) else { return nil } return bytes.withUnsafeBufferPointer({ UnsafePointer<T>($0.baseAddress).memory }) } That's it. 100% pure Swift, and probably about as efficient as you can get.
- fleitz 11y agoInterfaces which use the pattern: struct foo bar; foo_init(&bar); Can be very annoying in swift, it does play nicely with CoreFoundation which does not use this pattern
- mikeash 11y agoAs of Swift 1.something, C structs get imported with an empty initializer that sets everything to zero. So that pattern can be written in Swift as: var bar = foo() foo_init(&bar) No worse than the C version, as long as the unnecessary initialization is not a performance problem (which it usually isn't).