Last active
March 5, 2016 17:49
-
-
Save Ben-G/aa080de6d61f46bdc842 to your computer and use it in GitHub Desktop.
Compose Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of a thing I cannot do with types in Swift | |
struct Operation<Input, Output> { | |
// would also be nice to have a list of operations that yield different types (and be able to dynamically inspect the generic type) | |
var operations: [Operation<>] = [] | |
// This can be accomplished by using type erause | |
// would be nice to be able to leave a type hole here | |
var nextOperation: Operation<Output, _> // second generic argument can be any type from the perspective of this specific type | |
// only important aspect is that input of next operation matches output of this operation | |
// This can be accomplished partially by defining a function that only has a requirement for the first generic type | |
// when storing the value we however need to erase the type | |
// require `Output1` and `Output2` to be composed into a tuple output type `(Output1, Output2)` | |
// I don't think there's any way to accomplish this in Swift, except moving the composition from the type level | |
// to the value level? | |
init<Input, (Output1, Output2): Output, X>(op1: Operation<Input, Output1>, op2: Operation<Input, Output2>, nextOp: Operation<Output, X>) { | |
self.operations.append(op1) | |
self.operations.append(op2) | |
self.nextOperation = nextOp | |
} | |
init() {} | |
} | |
// High level idea: | |
let operationChain = Operation( | |
op1: Operation(op1: Operation<NoDependency,Int>, op2: Operation<NoDependency,String>, | |
nextOp: Operation<(Int,String), [Int]>())) | |
) | |
// second operation depends on two previous operations inputs |
jckarter
commented
Mar 5, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment