Add type syntax for expressing lookup (i.e. [ ]) types similar to function types


#1

In Kotlin of course you can declare a type as a function as in:

class Foo : (Int) -> String

which means that the class must declare a method of the form:

operator fun invoke(x: Int) : String

and of course you can declare a variable of this type meaning that you can use the invoke operation i.e. ( )

But it occurred to me that it might also be handy to do the same thing for the indexing or lookup operator, i.e. the square brackets [ ].

For example a declaration like this:

class Bar : [Int] -> String

Would mean that the class would be required to implement

operator fun get(i: Int) : String

and a variable of that type:

val bar : [Int] -> String = Bar()

can be used for lookup as in:

val s: String = bar[5]

The complication is that [ ] also can also map to set as well as get so there would need to be a way to declare whether the type also supports the set method operation. The best I can come up with is using a double-headed arrow to indicate the bidirectional association:

[Int] <-> String

would mean a type that has both of these functions:

operator fun get(i: Int) -> String
operator fun set(i: Int, s:String)

Not a common scenario, but I suppose you could also allow declaring that it only supports set, but not get:

[Int] <- String