How do arrays like Swift with a heterogeneous value work?

I am a C ++ programmer, just starting out with Swift. I watched Dave Abraham's WWCD discussion “Protocol-Oriented Programming in Swift” and I was intrigued by the fact that he could create a heterogeneous array of values ​​of types restricted by the protocol.

To use the example from the video, define a protocol Drawableand two structures that implement it:

protocol Drawable {
    func draw(renderer: Renderer) // Renderer is another protocol
}

struct Circle : Drawable {
    func draw(renderer: Renderer) {
        // Implementation
    }
}

struct Rectangle : Drawable {
    func draw(renderer: Renderer) {
        // Implementation
    }
}

You can define a Diagramas containing an array of Drawables

struct Diagram : Drawable {
    var elements: [Drawable] = []

    func draw(renderer: Renderer) {
        for e in elements {
            e.draw(renderer);
        }
    }
}

: elements ? Drawable , , . , " " ​​ / ?

+4
1

, , . , , .

-, , , ( Swift, Objective-C ).

-, :

protocol Foo { }

struct Bar: Foo { }

var fooArray = [Foo]()

fooArray.append(Bar())
fooArray.append(Bar())
fooArray.append(Bar())

let arrayElement = fooArray[0]

print(arrayElement)

... LLVM IR, swiftc -emit-ir unveil.swift > unveil.ir, IR, fooArray.append(Bar()):

%15 = getelementptr inbounds %P6unveil3Foo_* %3, i32 0, i32 1
store %swift.type* bitcast (i64* getelementptr inbounds ({ i8**, i64, { i64, i8*, i32, i32, i8*, %swift.type** (%swift.type*)*, %swift.type_pattern*, i32, i32, i32 }*, %swift.type* }* @_TMfV6unveil3Bar, i32 0, i32 1) to %swift.type*), %swift.type** %15, align 8
%16 = getelementptr inbounds %P6unveil3Foo_* %3, i32 0, i32 2
store i8** getelementptr inbounds ([0 x i8*]* @_TWPV6unveil3BarS_3FooS_, i32 0, i32 0), i8*** %16, align 8
%17 = getelementptr inbounds %P6unveil3Foo_* %3, i32 0, i32 0
call void @_TFV6unveil3BarCfMS0_FT_S0_()
%18 = bitcast %P6unveil3Foo_* %3 to %swift.opaque*
call void @_TFSa6appendurfRGSaq__Fq_T_(%swift.opaque* noalias nocapture %18, %swift.type* %14, %Sa* nocapture dereferenceable(8) @_Tv6unveil8fooArrayGSaPS_3Foo__)

LLVM IR, , Swift - .

, IR, Swift, :

leaq    __TWPV6unveil3BarS_3FooS_(%rip), %rax
leaq    __TMfV6unveil3Bar(%rip), %rcx
addq    $8, %rcx
movq    %rcx, -56(%rbp)
movq    %rax, -48(%rbp)
callq   __TFV6unveil3BarCfMS0_FT_S0_
leaq    __Tv6unveil8fooArrayGSaPS_3Foo__(%rip), %rdx
leaq    -80(%rbp), %rax
movq    %rax, %rdi
movq    -160(%rbp), %rsi
callq   __TFSa6appendurfRGSaq__Fq_T_

... , , .

, , SIL SILWitnessTable.h SILWitnessVisitor.h swift.org, swift/include/swift/SIL/, .

, ( , -, , , ), (, struct s) (read class es) . , , .

+4

Source: https://habr.com/ru/post/1620686/


All Articles