Skip to content

Instantly share code, notes, and snippets.

View uros-gardasevic's full-sized avatar
🏠
Working from home

Uros Gardasevic uros-gardasevic

🏠
Working from home
View GitHub Profile
@ole
ole / HeterogeneousDictionary.swift
Last active January 23, 2025 20:54
Code for my article "A heterogeneous dictionary with strong types in Swift" https://oleb.net/2022/heterogeneous-dictionary/
// A heterogeneous dictionary with strong types in Swift, https://oleb.net/2022/heterogeneous-dictionary/
// Ole Begemann, April 2022
/// A key in a `HeterogeneousDictionary`.
public protocol HeterogeneousDictionaryKey {
/// The "namespace" the key belongs to. Every `HeterogeneousDictionary` has its associated domain,
/// and only keys belonging to that domain can be stored in the dictionary.
associatedtype Domain
/// The type of the values that can be stored under this key in the dictionary.
associatedtype Value
/// Exponentiation operator
infix operator **
extension IntegerArithmetic {
/// Returns base ^^ exp
/// - parameter base: the base value
/// - parameter exp: the exponentiation value
static func **(base: Self, exp: Int) -> Self {
return repeatElement(base, count: exp).reduce(1 as! Self, *)
}