Support generic classes #223

Open
ribrdb opened this Issue May 23, 2013 · 2 comments

Projects

None yet

3 participants

@ribrdb
Contributor
ribrdb commented May 23, 2013

It should be possible to write generic classes in Mirah. Two possible syntaxes are:

class <A super Bar> MyContainer
  def <T extends A> foo(); end
end

or

class MyContainer[A > Bar]
  def [T > A] foo; end
end

Do we need to support specifying explicit type parameters for method calls? How would that look?

a = MyContainer.<Foo>new
a.<Foo>foo

I can't think of a syntax for that using [] that's distinguishable from the [] operator. Maybe if you required parens you could use

a = MyContainer.new[Foo]()
a.foo[Foo]()
@raum-dellamorte

I think this looks more like what's already done in Mirah.

class [ClassName]:<[Var] ^ [SuperClass]>
    def [method_name]:<T ^ [Var]>(); end
end

class MyContainer:<A ^ Bar>
    def foo:<T ^ A>(); end
end

[Class|object].[method]:<Type>()

a = MyContainer.new:<Foo>()
a.foo:<Foo>()

You could lose the <> but I think they make it easier to tell that it's a generic.

@shannah
Contributor
shannah commented Nov 17, 2014

I am working on a macro that allows me to add generics in method signatures. It works like this:

def myMethod:List
  signature 'List<String>'

...
end

def myMethodWithArgs(arg1:Map, arg2:List):List
  signature 'List<String>', 
    arg1: 'Map<String,MyClass>',
    arg2: 'List<MyOtherClass>'

   ...
end

The Java equivalent of the above would be:

public List<String> myMethod(){
    ...
}

public List<String> myMethodWithArgs(Map<String,MyClass> arg1, List<MyOtherClass> arg2){
    ...
}

This is just to fill the gap in creating more self-documenting APIs while we wait for true generics to be finalized. I'll post the code when done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment