1

In C, C++ and Objective-C you can compile part of an executable into its own "object file" and use it (and/or a library containing multiple object files) from any other code by including a "header file". Highly-templatized C++ code notwithstanding, a "header" typically contains just the declarations needed to validate the correctness of calling code (and assist the IDE with autocomplete, etc.).

But Swift does not have header files.

Now, apparently it is not currently possible to make a Swift static library, but in the future how would a situation like the above work, wanting to use some existing precompiled code from "new" source code, given that Swift does not have headers?

Would it work something like how [I infer] Java must work, where the compiled form can be introspected enough for the compiler to verify it is being used properly? Does Bitcode in addition to its intermediate representation also provide the necessary "protocol" for retaining such metadata?

If Apple were to port Cocoa to Swift (and keep it closed source), how would it then be "imported" into Swift apps?

Although, really, this question is not anything to do with "closed source" per se but rather trying to understand the boundaries around compilation units in Swift. Based on a similar question for the Go language, mine here could be re-phrased as: can you link to a pre-compiled Swift library without the source?

Community
  • 1
  • 1
natevw
  • 16,807
  • 8
  • 66
  • 90
  • Given that it's not currently possible... why don't we wait until it is currently possible, and then we'll see exactly how it works... but you know, you can also use libraries in languages that don't have `.h` files like C#, VB, Java, etc... so it'll probably work something like those... – nhgrif Jun 19 '15 at 02:30
  • And why would Apple start porting frameworks with 30 years worth of development (and more importantly testing) sunk into them in a language that isn't being entirely abandoned...? – nhgrif Jun 19 '15 at 02:32
  • Please don't get distracted by the specific examples. The real question, for someone who's studied how the toolchain works, is it conceivable to use a piece of Swift "code" without the compiler reloading the actual source. – natevw Jun 19 '15 at 20:07

2 Answers2

1

Well, just consider Apple's Swift libraries. They are closed-source, and you can use them fine and you can see pseudo-"headers" for the stuff in the library in the compiler. The exact mechanism of how this works is not currently publicly documented, but it must exist.

user102008
  • 30,736
  • 10
  • 83
  • 104
0

In addition to @user102008, the good new is, Swift will be open sourced by the end of this year, and even ported to Linux by Apple. While we can't guarantee it will always work that way (as Apple has poor records on those kind of issues), people will found suitable solutions within this even if Apple has no interests in doing so.

Even more, afaik, Swift objects were actually Objective-C objects. There'll not be that different to make Swift things work than Objective-C. (More details: http://www.eswick.com/2014/06/inside-swift/) After they were compiled, just do a class dump (or load it into a debugger such as IDA) and you can easily create a .h to make it work like normal static library or a framework.

Cai
  • 3,609
  • 2
  • 19
  • 39