@Claude31 , I thought I'd give you an update.
Played around with your suggestion a bit, and managed to get something close. This is the code I ended up with.
class Point
{
var x, y: Int
init(x: Int, y: Int)
{
self.x = x
self.y = y
}
}
class SubPoint: Point
{
var z: Int
init(x: Int, y: Int, z: Int)
{
self.z = z
super.init(x: x, y: y)
}
}
@dynamicMemberLookup
struct PassthroughWrapper<T>
{
var value: T
subscript(dynamicMember member: KeyPath<T, Int>) -> Int
{ return value[keyPath: member] }
}
let point = SubPoint(x: 2, y: 3, z: 5)
let wrapper = PassthroughWrapper(value: point)
print(wrapper.z)
Or, in my case
@dynamicMemberLookup
struct PassthroughWrapper<T: ElectronicComponent>
{
var value: T
subscript(dynamicMember member: KeyPath<T, Lead>) -> Lead
{ return value[keyPath: member] }
}
let resistorC = Resistor("R3", resistance: .init(value: 50, unit: .ohms))
let wrapper = PassthroughWrapper(value: resistorC)
print(wrapper[dynamicMember: \.output])
}
To elucidate, making the structure generic, things do work as I had been wanting – almost.
While everything appears to works as desired, it requires creating a wrapper for every component used. Which feels like it's negating the goal of creating connections less code.
Thanks to your suggestion, I have learned new things and new approaches. Perhaps I can repay your 1c, one day. ;-)