Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Interface Prototype #7025

Closed
wants to merge 1 commit into from
Closed

Conversation

tknopp
Copy link
Contributor

@tknopp tknopp commented May 29, 2014

This is prototype for the interface system outlined in #6975. The most important missing thing is the parse support that would need help from someone with experience in hacking the parser.

This PR adds three functions:

  • add_interface: Adds an interface to the interface table (this is what should be called from the parser)
  • verify_interface: Checks whether a type fulfills all interfaces
  • isinterface: Returns if a function belongs to an interface (this is an internal function used by showerror)

Further, the PR modifies showerror to check if a function is an interface and then displays the error message.

The interface implementation can be used in two different ways. Either one implements the interface and the calls verify_interface to directly validate that the interface is fulfilled. Or one skips the last step and still gets a nice error message.

In action:

julia> abstract A
julia> type B <: A end
julia> Base.add_interface(A,:length)
julia> length(B())
ERROR: Type B has to implement length to be a subtype of A!
no method length(B)

@kmsquire
Copy link
Member

Hi @tknopp, you seem to have a few extra commits here.

@lindahua
Copy link
Contributor

It looks like it goes through the interface verification whenever an instance of B is constructed? That would be a lot of runtime cost. It would be better to have this as an optional thing, so people can choose when to run the verification (e.g. during testing).

@tknopp
Copy link
Contributor Author

tknopp commented May 29, 2014

@kmsquire sorry, I have squashed the commits.

@lindahua: no it does not go through interface verification when B is constructed. Its done either explicitly by calling verify_interface or implicitly in the case of a MethodError.

@tknopp
Copy link
Contributor Author

tknopp commented May 30, 2014

One issue with my prototype is that it does not take into account parametric types. I am not entirely sure how this could be handled. Note that the issue is not that parametric types cannot be used but they cannot be used in the signature. The following is possible though:

julia> abstract Collection{T}
julia> Base.add_interface(Collection,:push!) # This does not restrict the signature
julia> Base.add_interface(Collection,:push!, :(Collection, Any)) # This restricts the signature but its not as specific as it could be

@vtjnash
Copy link
Sponsor Member

vtjnash commented Jun 26, 2019

This seems like a good thing to live in a package ecosystem (I think I've even seen some solvers that provide some related tests), but doesn't need to be in Base.

@vtjnash vtjnash closed this Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants