-
Notifications
You must be signed in to change notification settings - Fork 9
Alternative implementation of StaticStructural #10
Changes from all commits
8dff3e9
65741a1
5c26b94
3d58465
8339208
b20fb72
e52bb75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,57 +25,59 @@ public protocol Structural { | |
} | ||
|
||
/// Structural representation of a Swift struct. | ||
public struct StructuralStruct<Properties> { | ||
public var type: Any.Type? | ||
public struct StructuralStruct<BaseType, Properties> { | ||
public var type: BaseType.Type? | ||
public var properties: Properties | ||
|
||
public init(_ properties: Properties) { | ||
self.type = nil | ||
self.properties = properties | ||
} | ||
|
||
public init(_ type: Any.Type, _ properties: Properties) { | ||
public init(_ type: BaseType.Type, _ properties: Properties) { | ||
self.type = type | ||
self.properties = properties | ||
} | ||
} | ||
|
||
/// Structural representation of a Swift property. | ||
public struct StructuralProperty<Value> { | ||
/// | ||
/// Value corresponds to the original declared property type. | ||
/// WrappedValue is a declared property type with explicit property | ||
/// wrappers included in the type. | ||
public struct StructuralProperty<BaseType, Value, WrappedValue> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document Agreed that this is some unfortunate complexity. :-( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comment in b20fb72 |
||
public var keyPath: KeyPath<BaseType, Value>? | ||
public var name: String | ||
public var value: Value | ||
public var isMutable: Bool | ||
public var value: WrappedValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we can't get name out of KeyPath in a portable manner (there is a macOS-only solution that relies on NSExpression). This is intended because KeyPaths can express arbitrary multi-step field accesses. What's even more unsettling is that we use |
||
|
||
public init(_ value: Value) { | ||
self.name = "" | ||
self.value = value | ||
self.isMutable = false | ||
public var isMutable: Bool { | ||
return keyPath is WritableKeyPath<BaseType, Value> | ||
shabalind marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public init(_ name: String, _ value: Value) { | ||
self.name = name | ||
public init(_ value: WrappedValue) { | ||
self.name = "" | ||
self.keyPath = nil | ||
self.value = value | ||
self.isMutable = false | ||
} | ||
|
||
public init(_ name: String, _ value: Value, isMutable: Bool) { | ||
public init(_ keyPath: KeyPath<BaseType, Value>, _ name: String, _ value: WrappedValue) { | ||
self.name = name | ||
self.keyPath = keyPath | ||
self.value = value | ||
self.isMutable = isMutable | ||
} | ||
} | ||
|
||
/// Structural representation of a Swift enum. | ||
public struct StructuralEnum<Cases> { | ||
public var type: Any.Type? | ||
public struct StructuralEnum<BaseType, Cases> { | ||
public var type: BaseType.Type? | ||
public var cases: Cases | ||
|
||
public init(_ cases: Cases) { | ||
self.type = nil | ||
self.cases = cases | ||
} | ||
|
||
public init(_ type: Any.Type, _ cases: Cases) { | ||
public init(_ type: BaseType.Type, _ cases: Cases) { | ||
self.type = type | ||
self.cases = cases | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
type
Optional<BaseType.Type>
and not simplyBaseType.Type
? (Note: I realize this was the case before your change too.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look at implementation of
Zero
we need to be able to create an instanceStructuralStruct
based on known type parameters only.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But don't we now have
BaseType
? Couldn't we do something like: