What Language Is Swift Written In

What language is Swift written in?

The source code has just been made available on Github, and it appears that Swift itself is primarily written in C++, and its standard library is written in Swift.

For more info, see this press release from Apple, and the new Swift.org web site.

Does Swift compile to native code?

Yes, it compiles to machine language by way of LLVM Bitcode and, as @connor said, runs on top of the Objective-C runtime.

What is the Swift Language Version Xcode setting for? Because it still builds newer Swift code with an older version set

Prior to Swift 4, the version of the compiler and the language were one and the same. But since Swift 4, the compiler can run in a compatibility mode for previous Swift versions. check more info on compatibility modes in the Swift 4.0 release notes

The Xcode build setting SWIFT_VERSION set's the compiler flag -swift-version which is the language mode. From the swift compiler print out below this parameter only changes how the input is interpreted.

swiftc -h|grep 'Swift language version number'
-swift-version <vers> Interpret input according to a specific Swift language version number

Thus When you select Swift Language Version to 4.2, this does not mean use Swift 4.2 compiler. The compiler version will still be 5.1.3, the Swift Language Version setting instructs the compiler to run in Swift 4.2 compatibility mode. The compatibility mode means you may not need to modify your swift 4.2 code to use the new version of the compiler. Because the compiler running in compatibility mode allows Swift version 4.2 code to compile and run alongside code from version 5 and later.

compiler options

The Swift 5 compiler with compatibility mode can compile code written with either Swift 4 syntax, Swift 4.2 syntax, or Swift 5 syntax.

Here is a code example, create a file test.swift with code below:

//code written before siwft 5
let firstName = "michael jackson"
let offset = firstName.endIndex.encodedOffset

// Check swift version being used.
#if swift(>=5.2)
print("Hello, Swift 5.2")

#elseif swift(>=5.1)
print("Hello, Swift 5.1")

#elseif swift(>=5.0)
print("Hello, Swift 5.0")

#elseif swift(>=4.2)
print("Hello, Swift 4.2")

#elseif swift(>=4.1)
print("Hello, Swift 4.1")

#elseif swift(>=4.0)
print("Hello, Swift 4.0")

#endif

suppose the above code was written before swift 5 using the swift 4 compiler
this code will compile with no error's as shown below.

Sample Image

After swift 5 is released if you try to compile this code with Swift 5 compiler as shown below.

Swift 5 compiler

You will get the warning shown above since encodedOffset is deprecated in swift 5.

You could downgrade and use the swift 4 compiler or you can use the Swift 5 compiler in compatibility mode with the compiler flag -swift-version as shown below.

Swift 5 compiler with compatibility mode

It's important to note that Swift 4 compiler, and the Swift 5 compiler in Swift-4 compatibility mode are not the same thing. New swift 5 language features are normally available to the swift 5 compiler running compatibility mode. This allows developers to use the new features even when they can't upgrade to swift 5. The new Swift 5 features will not be available to the Swift 4 compiler.

What programming language is used for iPhone apps?

The language is Objective-C. If you know HTML and JavaScript you can look at PhoneGap.

Is the swift-2.0 compiler self-hosting?

What language is the swift-2.0 compiler written in? C++?

We can't yet say for sure if it's entirely in C++, because Apple hasn't released the source yet. We know it's built on LLVM so large parts are C++. Possibly some parts are written in Swift, but Apple hasn't revealed any Swift/C++ integration so I kind of doubt it.

Would a self-hosting swift be more powerful?

Unlikely. All Turing-complete languages are equally “powerful” in terms of what they can output. Some algorithms are easier to implement in language X than in language Y, but gcc (written in C and C++) and clang (written in C++) already implement many sophisticated optimizations, so this isn't a strong argument.

More desirable?

Maybe. Some languages are more “powerful” than others in terms of how conveniently you can achieve certain goals. For example, the pattern matching support in languages like ML and Haskell can be very useful in compiler implementation, but I don't know if Swift's pattern matching is particularly useful in that way.

How does Swift's compiler API compare with F#'s or https://github.com/dotnet/roslyn?

Impossible to say, since Swift has no public compiler API yet.



Related Topics



Leave a reply



Submit