Graphaello icon indicating copy to clipboard operation
Graphaello copied to clipboard

Explore dynamicMemberLookup and dynamicCallable to make code simpler

Open nerdsupremacist opened this issue 5 years ago • 1 comments

Something like this:

@dynamicMemberLookup
protocol Type: Target { }

extension Type {

    // make member key paths static for the type

    static subscript<T>(dynamicMember keyPath: KeyPath<GraphQLFragmentPath<Self, Self>, GraphQLFragmentPath<Self, T>>) -> GraphQLFragmentPath<Self, T> {
        return .init()
    }

    static subscript<T>(dynamicMember keyPath: KeyPath<GraphQLFragmentPath<Self, Self>, GraphQLPath<Self, T>>) -> GraphQLPath<Self, T> {
        return .init()
    }

}

@dynamicMemberLookup
struct GraphQLFragmentPath<TargetType: Target, UnderlyingType> {
    fileprivate init() {}

    // nested values inside arrays

    subscript<Value, Output>(dynamicMember _: KeyPath<GraphQLFragmentPath<TargetType, Value>, GraphQLPath<TargetType, Output>>) -> GraphQLPath<TargetType, [Output]> where UnderlyingType == [Value] {
        return .init()
    }

    subscript<Value, Output>(dynamicMember _: KeyPath<GraphQLFragmentPath<TargetType, Value>, GraphQLPath<TargetType, Output>>) -> GraphQLPath<TargetType, [Output]?> where UnderlyingType == [Value]? {
        return .init()
    }

    subscript<Value, Output>(dynamicMember _: KeyPath<GraphQLFragmentPath<TargetType, Value>, GraphQLFragmentPath<TargetType, Output>>) -> GraphQLPath<TargetType, [Output]> where UnderlyingType == [Value] {
        return .init()
    }

    subscript<Value, Output>(dynamicMember _: KeyPath<GraphQLFragmentPath<TargetType, Value>, GraphQLFragmentPath<TargetType, Output>>) -> GraphQLPath<TargetType, [Output]?> where UnderlyingType == [Value]? {
        return .init()
    }
}

and similar strategies for dealing with optionals...

The main reason this is not being used right now is the fact that autocomplete is broken. We should evaluate if there's a way to have autocomplete working and still bringing the amount of code waaaay down

nerdsupremacist avatar Apr 20 '20 17:04 nerdsupremacist

If this manages to work, we could get rid of _forEach and while we're at it I would also get rid of _fragment, since we probably can make it work somehow else

nerdsupremacist avatar May 12 '20 12:05 nerdsupremacist