Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For what is the new keyword?

Tags:

keyword

swift

I read the The Swift Programming Language iBook. In the keywords section they listed new. But in the section Summary of the Grammar I didn't found it.

For what is new used in Swift?

like image 201
Binarian Avatar asked Jun 23 '14 14:06

Binarian


People also ask

Why is new keyword used in JS?

The new keyword is used in javascript to create a object from a constructor function. The new keyword has to be placed before the constructor function call and will do the following things: Creates a new object. Sets the prototype of this object to the constructor function's prototype property.

What is the use of new keyword in C?

The new operator creates a new instance of a type. You can also use the new keyword as a member declaration modifier or a generic type constraint.

What new keywords return?

The new keyword constructs and returns an object (instance) of a constructor function. The new keyword performs following four tasks: It creates new empty object e.g. obj = { }; It sets new empty object's invisible 'prototype' property to be the constructor function's visible and accessible 'prototype' property.


2 Answers

It seems that new can be used to create an array:

let x = new Double[2]
x[0] = 3.1
x[1] = 4.2
println(x)
// Output: [3.1, 4.2]

But this is only of theoretical interest. It is not documented and should therefore not be used.

like image 129
Martin R Avatar answered Oct 18 '22 19:10

Martin R


It seems to have been removed.

I tried let x = new Double[2] but in Xcode 7 it now throws an error

Playground execution failed: OSX.playground:1:9: error: use of unresolved identifier 'new' let a = new [Double]

like image 21
Binarian Avatar answered Oct 18 '22 18:10

Binarian