2

I'm just wondering. As I understand, var and let can be anything and Swift automates the right type like in JavaScript. But when I try to set properties in a class I get an error when I don't specify the type.

var value1, value2 // Error: missing annotations

Well, I've read some references and the variable requires a type on declaration like var foo = 0. But in my class I have an init() which will set the variables to whatever I input when creating the object of the class.

So how should I achieve this? Is it even possible?

I saw the type typealias but that didn't work either.

Chris Page
  • 18,263
  • 4
  • 39
  • 47
Arbitur
  • 38,684
  • 22
  • 91
  • 128

3 Answers3

10

While both Swift and Javascript allow you to assign constants to variables without defining their type, there is a fundamental difference in the two languages. Swift is a strongly-typed, type-safe language while Javascript is not. From The Swift Programming Language -

“Swift is a type safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code expects a String, you can’t pass it an Int by mistake.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/au/book/swift-programming-language/id881256329?mt=11

So when you say

var welcomeMessage = "Hello" 

Swift infers that you want welcomeMessage to be a string and sets its type accordingly. Subsequently trying

welcomeMessage=3

will give run a compile-time error because you are assigning the incorrect type.

If you don't assign an initial value then Swift can't infer the type and you must specify it.

Javascript, on the other hand, will quite happily accept

var welcomeMessage="Hello"
welcomeMessage=3

because it isn't type safe and just tries to do the best it can with values it has. For example, if a string operation is performed on welcomeMessage after assigning 3 to it, Javascript would convert the value to "3" and then perform the operation.

While there are type safe extensions to Javascript, it isn't a fundamental part of the language the way it is with Swift

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • JavaScript is typesafe. “Typesafe” means that you can’t misinterpret the bytes in memory. When you operate on a value in JavaScript it will either succeed or throw an in-language error if there’s a type issue. “Typesafe” does not indicate whether or not there are explicit type declarations in source. – Chris Page Jun 03 '14 at 13:01
  • By the definition that Apple gives in their book, standard JavaScript is not type safe - as JavaScript is loosely typed you can get a runtime error by passing an arbitrary string where a number is expected. Swift will detect this at compile time and report an error, failing the build. – Paulw11 Jun 03 '14 at 13:13
  • Swift is both type safe and strongly typed. JavaScript is type safe but not strongly typed. Apple's book is not formal, it's colloquial. And it's colloquial intentionally because if it was formal most of the intended audience would not be able to understand it. In the specific paragraph that is quoted they could have been formal and still understandable, but they chose to be informal overall. – Analog File Jun 07 '14 at 02:36
  • Correction: when you say “strongly typed” you really mean “statically typed”. i.e., you mean that the compiler will issue errors about types. “Static” vs. “dynamic” indicates whether we’re talking about compile-time or runtime. “Strong” vs. “weak” indicates whether you can misinterpret data. C is statically typed, but weakly typed in that you can, for example, implicitly coerce with loss of data. – Chris Page Jun 09 '14 at 02:14
7

The types are only inferred if you assign a default value initially. From an example in the Language Reference, either declare the type:

var welcomeMessage: String

or assign an initial value that allows Swift to infer the type:

welcomeMessage = "Hello"

In the welcomeMessage example above, no initial value is provided, and so the type of the welcomeMessage variable is specified with a type annotation rather than being inferred from an initial value.

Ben-G
  • 4,996
  • 27
  • 33
  • I wander what's the default value of `var welcomeMessage: String` – Andrew Jun 07 '14 at 02:19
  • @Andraw There's no default value. You must write an `init` and initialize the variable there if you do not provide an initializer. The only exception is for optionals (which do not need initialization because ... they are optional). – Analog File Jun 07 '14 at 02:29
3

JavaScript is dynamically typed, meaning variables don't have a type assigned to them. Types are only associated with objects. So a variable can contain objects of any type.

Swift is statically typed on the other hand. Meaning variables have type. You can't place any object you want into a variable. The compiler will make sure you only place compatible objects into variables.

Swift has type inference which allow the compiler to figure out the type if you write:

var value1 = "foobar"

But just writing var value1 wouldn't let the compiler figure out what type the value1 variable is. JavaScript doesn't have this problem since variables don't have types.

Remember similar looking syntax does not mean the same semantics. Swift might look a bit like JavaScript syntax wise, but the meaning (semantics) of the keywords is quite different. If you want to simulate JavaScript:

var value1, value2

In Swift you would write:

var value1: AnyObject
var value2: AnyObject

That would allow you to put any object into the value1 and value2 variables.

Erik Engheim
  • 8,182
  • 4
  • 36
  • 51