New Issue: CG: single-dispatch-inspired interface features?

16800, “vasslitvinov”, “CG: single-dispatch-inspired interface features?”, “2020-12-02T01:46:14Z”

Here are some features we might want to consider incorporating that are inspired by Swift protocols, which bear similarity to Java interfaces.

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.

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);'

Restrict the types that can implement an interface. (This one is obvious in retrospect.) For example, require them to be non-nilable classes:

interface OnlyForClasses(type self: class) {...}

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.