Good evening, folks.
I recently encountered a need to define a structure to store a function and its related arguments into an instance.
struct Functor<Input, Output> {
//assuming only fuctions with one argument
let function: (Input) -> Output
let inputType: Input.Type
let outputType: Output.Type
let input: Input
let output: Output
init<Input, Output>(_ function: @escaping (Input) -> Output) {
self.function = function //there is already a compilation error
self.input = Input //???? And I have not a clue on how to store argument
}
}
Compilator says: error: cannot assign value of type '(Input) -> Output' to type '(Input) -> Output'
The reason I need this is to define custom properties for functions with something like this:
protocol FunctorProtocol: Equatable, Hashable {
//assiming only functions with one argument
associatedtype Output
associatedtype Input
var function: (Input) -> Output { get set }
var name: String? { get set }
var inputType: Input.Type { get set }
var outputType: Output.Type { get set }
var preconditions: [(Input) -> Bool] { get set }
var postconditions: [(Output) -> Bool] { get set }
func callAsFunction(_ a: Input) throws -> Output
//func bellow is impossible to implement. Provide a stub
func isSemanticalyIdentical(to another: Self) -> Bool
//(...)->(?)
func hasSignature(identicalTo another: Self) -> Bool
//(...)
func hasArgumentSignature(identicalTo another: Self) -> Bool
func hasSameReturnType(as another: Self) -> Bool
init<Input, Output>(
name: String?,
preconditions: [(Input) -> Bool]?,
postconditions: [(Input) -> Bool]?,
inputType: Input,
outputType: Output,
function: (Input)->(Output)
)
}
To then implement bellow:
@CustomStorage(constraints: [{ $0 is Numeric }]) var someNumber: Any = 5 //can store any variable of type Numeric
try? assign(value: 0.5, to: someNumber) //success
try? assign(value: "String", to: someNumber) //failure
Can anybody, please, hint me, if I even doing this correct?
I think you’re shadowing the generic types Input and Output in your initializer. You don’t need to declare new genetic parameter types in it, you should use the ones inherited from the type.
Thank you with your responce, I have fixed the code so it is working. But I actualy broke swift in other way . I am using online swift compiler, to say.
Here is the minimal reproducible example
and the other is in the implementation of invoke; in case the array of preconditions is empty, this implementation has no return statement: you can fix it by tacking an extra return statement at the bottom of the function.
func invoke(with a: Input) throws -> Output {
for f in preconditions {
if f(a) == false { throw Unprocessable.preconditionError; break }
else { return self.function(a) }
}
return self.function(a)
}
About the isSignature function: why do you need a runtime check? You can turn it into a compile-time error using a where clause. At this point you don't even need this function anymore, as you can constraint the types of the rest of your logic.
func isSignature<
T: DistilledFunctorProtocol,
U: DistilledFunctorProtocol
>(
of some: T,
identicalTo another: U
) where T.Input == U.Input, T.Output == U.Output
{}
About the compiler error: I copy pasted your code in a Xcode 11.2.1 playground and I didn't get the compiler crash you got.
Ok, got it. It is possible to specialize some cases of isSignature with where. But because some Functor types are generic and may be created at runtime, it is necessary to use more generalized solution, I think