You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this case, it would be better to avoid declaring `MyType` with a field `a::Array` and instead declare the field as `a::Array{T,N}` or as `a::A`, where `{T,N}` or `A` are parameters of `MyType`.
360
360
361
+
The previous advice is especially useful when the fields of a struct are meant to be functions, or more generally callable objects.
362
+
It is very tempting to define a struct as follows:
363
+
364
+
```julia
365
+
struct MyCallableWrapper
366
+
f::Function
367
+
end
368
+
```
369
+
370
+
But since `Function` is an abstract type, every call to `wrapper.f` will require dynamic dispatch, due to the type instability of accessing the field `f`.
371
+
Instead, you should write something like:
372
+
373
+
```julia
374
+
struct MyCallableWrapper{F}
375
+
f::F
376
+
end
377
+
```
378
+
379
+
which has nearly identical behavior but will be much faster (because the type instability is eliminated).
380
+
Note that we do not impose `F<:Function`: this means callable objects which do not subtype `Function` are also allowed for the field `f`.
381
+
361
382
### Avoid fields with abstract containers
362
383
363
384
The same best practices also work for container types:
function$t{T,S}(data) where {T,S<:AbstractMatrix{T}}
@@ -854,9 +853,9 @@ for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular, :UnitUpperTr
854
853
end
855
854
end
856
855
857
-
errorbounds(A::AbstractTriangular{T,<:AbstractMatrix}, X::AbstractVecOrMat{T}, B::AbstractVecOrMat{T}) where {T<:Union{BigFloat,Complex{BigFloat}}} =
856
+
errorbounds(A::AbstractTriangular{T}, X::AbstractVecOrMat{T}, B::AbstractVecOrMat{T}) where {T<:Union{BigFloat,Complex{BigFloat}}} =
858
857
error("not implemented yet! Please submit a pull request.")
859
-
functionerrorbounds(A::AbstractTriangular{TA,<:AbstractMatrix}, X::AbstractVecOrMat{TX}, B::AbstractVecOrMat{TB}) where {TA<:Number,TX<:Number,TB<:Number}
858
+
functionerrorbounds(A::AbstractTriangular{TA}, X::AbstractVecOrMat{TX}, B::AbstractVecOrMat{TB}) where {TA<:Number,TX<:Number,TB<:Number}
0 commit comments