Skip to content

Instantly share code, notes, and snippets.

View gsoc-internals.md

Provide documentation for various Swift internals

Motivation

Swift is a very successful combination of a compiled language with numerous high-level features. This means that, on the one hand, the behaviour of the code is very predictable, as there’s only one widely used Swift compiler. On the other hand, many such high-level features don’t directly translate to the present hardware’s instructions and as such require substantial language runtime and compiler magic to be able to work.

Unfortunately, there is currently little to no way for a typical language user — that is, anybody who isn’t familiar with the compiler’s implementation — to get acquainted with various implications of using most of the features of Swift. One either needs to familiarise themselves with different compilation stages and representations (SIL, LLVM IR) or rely on scarce, topical information present on the Swift forums or in videos from the WWDC or the LLVM conferences. This not only contributes to users having a harder time debu

View hmm.swift
struct State {
}
struct Guard {
private var state = State()
private var condition = false
View bar.swift
enum WineColor {
case red
case white
var description: String {
switch self {
case .red:
return "sauvignon"
case .white:
View SceneDelegate.swift
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
// MARK: View controllers
private lazy var dashboardVC = DashboardViewController()
private lazy var newTransactionVC = NewTransactionViewController()
private lazy var navigationController = UINavigationController(rootViewController: dashboardVC)
private lazy var categoryEditorVC = CategoryEditorViewController(mode: .creation)
View CalendarHelpers.swift
import UIKit
class GridView<Cell: GridViewCell>: UIView {
struct Position: Hashable {
let row: Int
let column: Int
}
View mergelists.py
class Node:
def __init__(self, initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
View make-image.swift
import Foundation
import CoreGraphics
import ImageIO
import AppKit
func makeImage() {
let ptr = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: 4 * 1000 * 1000)
for i in 0..<(4 * 1000 * 1000) {
ptr[i] = 255
View multipleDecodingPaths.swift
import Foundation
let jsonStringOne = """
{
"fruit": "Apple",
"size": "Large",
"color": "Red"
}
"""
View CyclicView.swift
import UIKit
class CyclicView<Cell: CyclicViewCell, DataSource: CyclicViewDataSource>: UIScrollView where Cell.Model == DataSource.Model {
private var pool = Set<Cell>()
unowned var dataSource: DataSource
private var buffer = ViewBuffer<Cell>()