Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
Go
Haskell
HLSL
Hook
Hylo
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
swift source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
x86-64 swiftc 3.1.1
x86-64 swiftc 4.0.2
x86-64 swiftc 4.0.3
x86-64 swiftc 4.1
x86-64 swiftc 4.1.1
x86-64 swiftc 4.1.2
x86-64 swiftc 4.2
x86-64 swiftc 5.0
x86-64 swiftc 5.1
x86-64 swiftc 5.10
x86-64 swiftc 5.2
x86-64 swiftc 5.3
x86-64 swiftc 5.4
x86-64 swiftc 5.5
x86-64 swiftc 5.6
x86-64 swiftc 5.7
x86-64 swiftc 5.8
x86-64 swiftc 5.9
x86-64 swiftc nightly
Options
Source code
protocol Foo { } extension Foo { func greeting() -> String { "Hello, I am a Foo." } } protocol Bar: Foo { } extension Bar { func greeting() -> String { "Hello, I am a Bar." } } struct Baz: Bar { } struct Qux: Bar { func greeting() -> String { "Helllo, I'm Qux!" } } func greet1<T: Foo>(_ foo: T) { // All we know about T is that it's a Foo // so we always call the Foo version. print(foo.greeting()) } func greet2<T: Bar>(_ bar: T) { // All we know about T is that it's a Bar // which is a more refined protocol than Foo // so we always call the Bar version. print(bar.greeting()) } do { let baz = Baz() let qux = Qux() // No dynamic dispatch ever occurs in this entire program. greet1(baz) greet2(baz) greet1(qux) greet2(qux) // Bar is more specific than Foo, so we use the Bar version. print(baz.greeting()) // Qux has it's own version of greeting(), so we use that one // because it's the most specific. print(qux.greeting()) }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree