chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.8k stars 421 forks source link

CG: single-dispatch-inspired interface features? #16800

Open vasslitvinov opened 3 years ago

vasslitvinov commented 3 years ago

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.

record R: SomeInterface {   // syntactic sugar for 'implements SomeInterface(R);'
  ......
}

(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 OnlyForClasses(type self: class) {...}
interface ArgSatisfiesConstraint(type self) where implements AnotherInterface(self) {...}

(interface-composition) Interpret & on two interfaces as "require both". For example:

// syntactic sugar for 'where implements FirstInterface(arg.type) && implements SecondInterface(arg.type)'
proc myCGfunction(arg: FirstInterface & SecondInterface) {
  .....
}

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 interpret MyClass as an implicit interface that is implemented by all subclasses of MyClass.

lydia-duncan commented 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.

vasslitvinov commented 3 years ago

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!