Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "Element" type?

Reading Swift Programming Language book I've seen number of references to type Element, which is used to define type of collection items. However, I can't find any documentation on it, is it class, protocol? What kind of functionality/methods/properties it has?

struct Stack<Element>: Container {
    // original Stack<Element> implementation
    var items = [Element]()
    mutating func push(_ item: Element) {
        items.append(item)
    }
    ...
like image 959
Pablo Avatar asked Apr 15 '18 08:04

Pablo


People also ask

How many types of element are there?

There are now 118 known elements. In this context, "known" means observed well enough, even from just a few decay products, to have been differentiated from other elements.

How do you know what type of element you are?

There are two properties that can be used to identify an element: the atomic number or the number of protons in an atom. The number of neutrons and number of electrons are frequently equal to the number of protons, but can vary depending on the atom in question.

What is element type in Abaqus?

Two types of shell elements are available in ABAQUS: conventional shell elements and continuum shell elements.

Is 0D an element type or not?

0D elements are commonly known as point elements. In this example, the W shape has a center of gravity, and with a 0D element, I can represent the W shape as a point of mass. Other types of point elements include: Grounded spring, grounded damper, and grounded bush. 1D elements are commonly known as line elements.


2 Answers

If we tried trace the idea of how we do even get Element when working with collections, we would notice that it is related to the Iterator protocol. Let's make it more clear:

Swift Collection types (Array, Dictionary and Set) are all conforms to Collection protocol. Therefore, when it comes to the Collection protocol, we can see that the root of it is the Sequence protocol:

A type that provides sequential, iterated access to its elements.

Sequence has an Element and Iterator associated types, declared as:

associatedtype Element

associatedtype Iterator : IteratorProtocol where Iterator.Element == Element

You could review it on the Sequence source code.

As shown, Iterator also has an Element associated type, which is compared with the sequence Element, well what does that means?

IteratorProtocol is the one which does the actual work:

The IteratorProtocol protocol is tightly linked with the Sequence protocol. Sequences provide access to their elements by creating an iterator, which keeps track of its iteration process and returns one element at a time as it advances through the sequence.

So, Element would be the type of returned element to the sequence.


Coding:

To make it simple to be understandable, you could implement such a code for simulating the case:

protocol MyProtocol {
    associatedtype MyElement
}

extension MyProtocol where MyElement == String {
    func sayHello() {
        print("Hello")
    }
}

struct MyStruct: MyProtocol {
    typealias MyElement = String
}

MyStruct().sayHello()

Note that -as shown above- implementing an extension to MyProtocol makes MyElement associated type to be sensible for the where-clause.

Therefore sayHello() method would be only available for MyProtocol types (MyStruct in our case) that assign String to MyElement, means that if MyStruct has been implemented as:

struct MyStruct: MyProtocol {
    typealias MyElement = Int
}

you would be not able to:

MyStruct().sayHello()

You should see a compile-time error:

'MyStruct.MyElement' (aka 'Int') is not convertible to 'String'

The same logic when it comes to Swift collection types:

extension Array where Element == String {
    func sayHello() {
        print("Hello")
    }
}
like image 192
Ahmad F Avatar answered Oct 06 '22 17:10

Ahmad F


Here is the definition in the Apple documentation

Element defines a placeholder name for a type to be provided later. This future type can be referred to as Element anywhere within the structure’s definition.

like image 41
Kamran Avatar answered Oct 06 '22 19:10

Kamran