Gridap.Inference
Gridap.Inference — ModuleThis module provides a set of helper function to safely infer return types of functions.
In Gridap, we rely as less as possible in type inference. But, when needed, we adopt the following mechanism in order to compute returned types. We do not rely on the Base._return_type function.
This module exports following functions:
Gridap.Inference.return_type — Methodreturn_type(f::Function, Ts::Vararg{Any,N} where N) -> DataType
Returns the type returned by function f when called with arguments of the types in Ts.
The underlying implementation uses the function testargs to generate some test values in order to call the function and determine the returned type. This mechanism does not use Base._return_type. One of the advantages is that the given function f is called, and thus, meaningful error messages will be displayed if there is any error in f.
Gridap.Inference.return_type_broadcast — Functionreturn_type_broadcast(f::Function,Ts::DataType...) -> DataTypeLike return_type, but when function f is used in a broadcast operation.
Gridap.Inference.testargs — Functiontestargs(f::Function,Ts::DataType...) -> TupleReturns a tuple with valid arguments of the types in Ts in order to call function f. It defaults to testvalues(Ts...), see the testvalues function. The user can overload the testargs function for particular functions if the default test arguments are not in the domain of the function and a DomainError is raised.
Examples
For the following function, the default test argument (which is a zero) is not in the domain. We can overload the testargs function to provide a valid test argument.
using Gridap.Inference
import Gridap.Inference: testargs
foo(x) = sqrt(x-1)
testargs(::typeof(foo),T::DataType) = (one(T),)
return_type(foo, Int)
# output
Float64Gridap.Inference.testargs_broadcast — Functiontestargs_broadcast(f, Ts)
Gridap.Inference.testvalue — Functiontestvalue(::Type{T}) where TReturns an arbitrary instance of type T. It defaults to zero(T) for non-array types and to an empty array for array types. This function is used to compute the default test arguments in testargs. It can be overloaded for new types T if zero(T) does not makes sense.
Gridap.Inference.testvalues — Functiontestvalues(Ts::DataType...) -> TupleReturns a tuple with test values for each of the types in Ts. Equivalent to map(testvalue,Ts).