Having reference semantics counterpart of value types in an easier way


(Amir Abbas Mousavian) #1

Having reference semantics counterpart of value types in an easier way

Introduction

···

———————

Now swift has its basic datatypes as value semantics. Arrays, Int.
Dictionaries, Sets and … are struct types without any kind of inheritance.

To compensate it, programmers usually use NS classes of these types. We are
about to introducing a new way to have reference types in a more swifty way
rather than using Foundation framework.

Swift 3 introduce omitting NS prefix from Foundation framework but leaves
NS prefix for some types with struct counterparts like Array, which may
leads to inconstancy for developers, and many datatypes remain classes (e.g
URL) which should be struct in swift paradigm while they remained classes

Motivation

———————

Programmers usually have to memorize the counterpart of Swift datatypes in
Foundation easily. Besides many datatypes remain classes (e.g URL) which
should be struct in swift paradigm while they remained classes

Proposed solution

———————

We can match existing classes to their value-type counterpart like this:

class Array: NSArray as reftype of Array {

}

The compiler should check all Array struct functions are implemented in
this new class type otherwise throw an compile error. For de novo
declarations:

class ValType as reftype of ValType {

}

here, the only thing user has to is reimplement mutating functions here, or
use Bridgeable protocol. I've no idea here yet

To create a reference Array (former NSArray) programmer can simply do:

let NSArrayInstance: *Array[NSObject] = [];

or

let NSArrayInstance: reftype Array[NSObject] = [];

Alternative solution

———————

Leave Foundation framework as it is by now.