Open vasslitvinov opened 3 years ago
Allow a class/record to specify (some/all of) the interface(s) it implements using the class inheritance syntax
This seems potentially confusing to me, do other languages have an argument for why that's an acceptable price?
Restrict the types that can implement an interface. (This one is obvious in retrospect.)
Do you mean "allow an interface to restrict the types that can implement it"? My first read of that sentence was "only allow interfaces on classes", which isn't something I would support.
do other languages have an argument for why that's an acceptable price?
I have not seen any discussions of that. Maybe others do not consider this aspect confusing?
To me the confusing part is that the functions specified by the interface are actually methods.
Restrict the types that can implement an interface.
This is indeed poor phrasing. I replaced with your suggestion, thank you!
Main issue: #8629
Here are some features we might want to consider incorporating that are inspired by Swift protocols, which bear similarity to Java interfaces. Note also that Rust traits allow only a single argument.
(only-methods) An interface specifies only methods, to be provided by the implementing type. A main benefit is that the requirements are syntactically similar to the declarations of primary methods in a record or a class, where the
this
formal does not need to be specified explicitly. The downside is that this rules out non-method functions and would make multi-parameter interfaces asymmetric.(inheritance-syntax) Allow a class/record to specify (some/all of) the interface(s) it implements using the class inheritance syntax, ex.
(type-restrictions) Allow an interface to restrict the types that can implement it. (This one is obvious in retrospect.) For example, require them to be non-nilable classes:
(interface-composition) Interpret
&
on two interfaces as "require both". For example:In Swift,
&
can also be applied to a type and an interface, ex.arg: MyInterface & MyClass
. In Chapel, we can consider this as a mix of constrained and unconstrained generics or we can interpretMyClass
as an implicit interface that is implemented by all subclasses ofMyClass
.