Skip to content

Commit e9c28d5

Browse files
committed
Add _implements also for instances of types, not just types
1 parent 4678b41 commit e9c28d5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/fundamental_interface.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,20 @@ function hom end
470470
#
471471
_implements(::Type{T}, f::Any) where {T} = hasmethod(f, Tuple{T})
472472

473+
# Alternatively, the first argument can be a concrete object. By default we
474+
# then redispatch to the type based version. But one may also choose to
475+
# implement custom methods for this: certain operations will only work for
476+
# *some* instances. E.g. for `Z/nZ` it may happen that for `n` a prime we can
477+
# perform a certain operation, but not if `n` is composite.
478+
#
479+
# In that case the recommendation is that `_implements` invoked on the type
480+
# returns `false`, but invoked on a concrete instance of a type, it may use
481+
# specifics of the instance to also return `true` if appropriate.
482+
function _implements(x::T, f::Any) where {T}
483+
@assert !(x isa Type) # paranoia
484+
return _implements(T, f)
485+
end
486+
473487
# helper for `_implements` which checks if `f` has a method explicitly for
474488
# a concrete type `T` (i.e. not a generic method that can be specialized to `T`
475489
# but really one that is implement for `T` and `T` only).

0 commit comments

Comments
 (0)