Skip to content

Instantly share code, notes, and snippets.

@Vaguery
Created October 8, 2021 17:27
Show Gist options
  • Save Vaguery/20bbf15ceb4d5e8fb862be24e5c75951 to your computer and use it in GitHub Desktop.
Save Vaguery/20bbf15ceb4d5e8fb862be24e5c75951 to your computer and use it in GitHub Desktop.
trying to understand Swift subclass collections
//
// KickInterpreter.swift
// Kick
//
// Created by Bill Tozier on 10/7/21.
//
import Foundation
protocol Pushable {
associatedtype Item
var push_type: String { get }
var value : Item { get }
}
protocol PushItem {
}
// push items
struct PushAtom<T> : PushItem,Pushable {
var value: T
var push_type: String
init(_ v: T, _ name: String = "") {
value = v
push_type = name
}
}
struct PushBlock: PushItem,Pushable {
var value: [PushItem]
var push_type = "codeblock"
init(_ v: PushItem ...) {
value = v
}
}
struct Foo {
var bar: String
init(_ bar:String) {self.bar = bar}
}
let b1 = PushAtom<Int64>(11)
let b2 = PushAtom<Double>(2.2)
let b3 = PushAtom<Foo>(Foo("baz"))
let block1 = PushBlock(b1,b2)
let block2 = PushBlock(block1,b3)
print(block.value[0].value)
// ERROR: Value of type 'PushItem' has no member 'value'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment