Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Swift a dynamic or static language?

Tags:

swift

I'm just curious if Swift is dynamic like php or static, I mean can I generate classes while an application is running?

like image 479
Moises Zermeño Avatar asked Apr 28 '15 16:04

Moises Zermeño


People also ask

Is Swift static type language?

Swift is a statically typed language, meaning that the type of every property, constant and variable that we declare needs to be specified at compile time.

Is it a static or dynamic language?

Statically typed languages perform type checking at compile-time, while dynamically-typed languages perform type checking at run-time. Statically-typed languages require you to declare the data types of your variables before you use them, while dynamically-typed languages do not.

Is Objective-C dynamic language?

Objective-C uses the id data type to represent a variable that is an object without specifying what sort of object it is. This is referred to as dynamic typing. Dynamic typing contrasts with static typing, in which the system explicitly identifies the class to which an object belongs at compile time.

Is Ruby a dynamic language?

Ruby is a dynamic language, which means that types are checked when the code is run. If you try to call a method on an object that does not exist, the compiler will not complain, you'll only find out about that error when the code is executed and you get a NoMethodError .


2 Answers

It is static - very static. The compiler must have all information about all classes and functions at compile time. You can "extend" an existing class (with an extension), but even then you must define completely at compile time what that extension consists of.

Objective-C is dynamic, and since, in real life, you will probably be using Swift in the presence of Cocoa, you can use the Objective-C runtime to inject / swizzle methods in a Swift class that is exposed to Objective-C. You can do this even though you are speaking in the Swift language. But Swift itself is static, and in fact is explicitly designed in order to minimize or eliminate the use of Objective-C-type dynamism.

like image 70
matt Avatar answered Sep 18 '22 00:09

matt


Swift itself, is statically typed. When used with Cocoa, you get access to the objective-c runtime library which gives you the ability to use dynamic classes, messages and all. This doesn't mean the language itself is dynamically typed. You could do the same with C or any other language that supports a bridge to C by using libobjc.A.dylib.

like image 22
Rad'Val Avatar answered Sep 19 '22 00:09

Rad'Val