or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
The purpose of this document is to serve as a focused basis of discussion for the design of an interfaces system for Julia. It is intended to capture the outcome of several discussions I've had with different groups of people over the past few months. The design described herein is intended to be the minimal basis on which to build further. It does not capture everything that everyone wants to do, but hopefully it captures a common sense of what most people agree on is a reasonable place to start.
Guiding Principles
The first question to answer here is what the purpose of an interface system is. There are differing opinions on this and different goals, but I think the most pressing issue is what I like to call the "compositional whodunnit". What I mean by that is that when composing different packages providing abstract functionality, it is easy to run into situations where something breaks due to leaky abstractions. In such situations, it is extremely hard to differentiate between:
This becomes expontentially more difficult the more packages and interfaces are involved, and as such issues of this nature often require triage by developers familiar with large fractions of the ecosystem.
In some ways, the fundamental problem here is a lack of documentation.
We do not have a practice of writing down what our interfaces are, so
even those who wish to write code against an interface are generally unable to do so.
In my personal opinion, this is the major part of problem and an opinionated and structured format for documenting interface expectations and requirements is already 90% of a solution, though we can and should of course try to do better still.
That said (and I think this is the main point of my argument), because of this, to the extent that there is a conflict between the ability to clearly specify an interface and the ability to check that a particular implementation satisfies an interface, we should err on the side of expressability, rather than verification. This is in some sense the exact opposite trade off that static languages take, where the expressivity is exactly the expressivity of the interface.
With these thoughts in mind, I would like to propose the following set of principles:
An implementation roadmap
Based on various discussions, I think we should being with interfaces that are simple list of methods (and in an immediate second step their return types). The initial semantics of an interface would be that an interface is deemed satisfied if, for the given type, it is not possible for the signature to throw a MethodError. As a notional syntax, we might consider:
The reason for choosing the absence of a MethodErorr, rather than a weaker notion like method existence is that it bootstraps more nicely to correctness checking users of an interface. For example, we might have a function like:
We could have a function like (again, syntax notional)
check(get_all, Length | Indexable)
that would go through and check, whether (under the assumption that the methods of the interfaces themselves do not throw method errors), the function get_all can throw a method error. In particular, this would then allow for interface composition.An additional important consideration is that we have the ability (as of last year - added precisely for this purpose) to ask the compiler to check whether a given signature has the possiblity to throw a MethodError, as well as the ability to provide nice user-facing printing of such analysis (using JET).
I thus propose the following roadmap:
Ability to specify/check a list of methods (no return types)
MethodError
is in the compilers inferred exctypeAbility to check an implementation against an interface
Add return types to the above
Design a system to associate interfaces with abstract types
Write interfaces for Base and see how things work out
Integrate interfaces into Base's existing
MethodError
error hinting.Importantly, none of these require additions to base (with the possible exception of step 2, which is supported in principle, but nobody has every really tried).
That said, I know that this is not powerful enough to implement many of the different kinds of interfaces that people want, for example, dependent return types
getindex(::T, ::Int)::eltype(T)
, value constraintslength(size(T)) == ndims(T)
, etc. Those additions are comptabile with this notion of interfaces, but are deliberately excluded from the MVP because they are harder to do.Thoughts on associating interfaces with abstract types
I propose that we use method tables for type-interface association. The reason for this is that julia programmers are already used to thinking about method tables and how they merge between different packages. An example way to do this might be to say something like (syntax notional):
Then a macro like
@check_all_interfaces(MyPackage)
would go through and ask the compiler to check all interfaces declared as above inMyPackage
.Importantly, a definition like this would allow opt out for special snowflake types like:
In addition, we could adjust the
MethodError
handler to give more informative error message when a concrete interface violation is detected (however, this is trickier than it may seem, because the MethodError being thrown may not necessarily be the one that violates the interface itself (rather the fact of a MethodErorr being thrown there could violate some interface higher in the stack) - so I would suggest punting on this for now).Is this the same as traits?
Traits and interfaces are somewhat orthogonal. To my mind, the biggest differences is that for traits people generally want to be able to use them to affect the runtime semantics of the program. This proposal does not do that. However, it is of course possible to use any of the existing trait systems and use this proposal to specify their semantics. The biggest issue for a dedicated traits system is that I don't think there is sufficient design space between what is current achievable with traits using macros (esp when enhanced with this proposal) and the place where you start wanting a compiler to do algorithmic selection for you (which needs a completely seprate mechanism). That said, we can revisit that question at a later time.