[Types-sig] parameterization and syntax

Tony Lownds Tony Lownds <tony@metanet.com>
Mon, 3 Jan 2000 20:51:40 -0800 (PST)


On Mon, 3 Jan 2000, Greg Stein wrote:

> 
>   interface Foo{_X,_Y}(Bases):
>     ...
>   def bar(x: Foo{Int, String}):
>     ...
> 

Hmm, how about an "of" keyword:

interface (_X,_Y) Foo(Bases):
  ...

def bar(x: Foo of (Int, String)):
  ...

That seems much more readable to me. A caveat is that handling a single,
tuple argument is tricky:

interface (_X) A: ...
interface (_X,_Y) B: ...

def f(a: A of (Int, Int)): ...
def g(b: B of (Int, Int)): ...

It'd be nice if the "def g" above could work but that means you have to
look at the interface to see whether (Int, Int) will be assigned to
one type name or split into two.

Anyway, there are the pros and cons. IMO the extra readability
"of" affords is worth it!

> ==> we could introduce a token, such as "param":
> 
>       def Add(x: param _X, y: param _X) -> param _X:
> 
>     This is a bit wordy, but works. If we want to cut down on the words,
>     then where does the token go? The first occurrence? That creates a
>     weird "the first one is special" rule that doesn't normally occur in
>     Python.
> 

Using $ instead of "param" as a marker would make it less wordy and
easier to put in the grammar but also more perl-like. IMO the param marker
should go everywhere that its used, just like you show in the example. 


> ==> other ideas? I'm out

how about simply not using parameters:

  def Add(x: Any, y: type of x) -> type of x:
    "add two numbers"

Ignoring the "of" business, you don't introduce a name for a
type parameter - you just refer to the type of another formal argument.


> Note that it will also be important to mix parameterized interfaces and
> functions:
> 
>   interface MySequence{_X}:
>     def getitem(x: _X{_Y}) -> _Y:
>       "doc"
> 
>   f = MyClass() ! MySequence{List}
>   l = something() ! List{Int}
>   z = f.getitem(l) ! Int
> 

This can be worked into the scheme above if one can reference the types a
parameterized type is bound to via special member variables:

   interface (_X) MySequence:
     def getitem(x: _X) -> _X.__type_param__[0]:
       "doc"

-Tony