Skip to content

Instantly share code, notes, and snippets.

@ivarne
Forked from tknopp/interfaces.jl
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivarne/026e9fb6f5a2d7e91f4f to your computer and use it in GitHub Desktop.
Save ivarne/026e9fb6f5a2d7e91f4f to your computer and use it in GitHub Desktop.
const _interfaces = Dict{Function,Vector{DataType}}()
function addInterface(d::DataType, f::Function...)
for g in f
if haskey(_interfaces, g)
push!(_interfaces[g], d)
else
_interfaces[g] = DataType[d]
end
end
end
function checkInterface(d::DataType)
for (f,itypes) in _interfaces
isInInterfaceTable = false
local parentType
for it in itypes
if d <: it
isInInterfaceTable = true
parentType = it
end
end
isInMethodTable = false
if isInInterfaceTable
mt = methods(f)
for m in mt
if d <: m.sig[1]
isInMethodTable = true
end
end
end
if isInInterfaceTable && !isInMethodTable
error("Interface not implemented!\n $d has to implement ", string(f), " in order to be subtype of $parentType !")
end
end
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment