#902 Returning This[]

kaushik Sat 9 Jan 2010

I can return an instance of the type being used by returning "This" from a method. Is there a way to return a list of instances of type being used. returning This[] does not seem to work.

brian Sun 10 Jan 2010

That would be a nice feature, but I'd have to give that some thought regarding covariance and contravariance. I'm surprised I haven't run across that use case myself.

fury Fri 15 Jan 2010

Would this be the equivalent of keeping a static list of all the instances created?

kaushik Fri 15 Jan 2010

No, this would just mean that the method would always return a list of instances of the Type being used

eg.,

class Parent{
  This[] values(){
  ..
  } 
} 

class Child : Parent{

}

now, Child().values(), will return Child[]. I see this as particularly useful with type inference. you can do

childs := Child().values()

instead of

Child[] childs := Child().values()

kaushik Fri 22 Jan 2010

On similar lines, could we allow a method to return "This?" to specify that it can return an instance of the current type or null..

brian Fri 22 Jan 2010

Promoted to ticket #902 and assigned to brian

I'm not sure this is something I'll actually tackle. But I think it makes sense to officially track as a feature request. Support for the following return types:

  • This?
  • This[]
  • This?[]

tactics Fri 22 Jan 2010

What about other kind of This-based types, such as Uri:This or This:Str or |This->Foo|. If we add support for types parametrized over This, we might as well generalize it to every polymorphic type.

brian Fri 22 Jan 2010

good point - that is exactly why this sort of explodes in complexity

jodastephen Fri 22 Jan 2010

Just adding This?, This[] and This?[] would be a major step forward. I wouldn't have thought This? was too hard...

tactics Fri 22 Jan 2010

good point - that is exactly why this sort of explodes in complexity

I never gave any thought to the necessity of this feature. But I thought I'd throw it out there for completeness.

andy Fri 22 Jan 2010

What would be the use case for this feature? I could see This?, but not sure I see when I would want to return a list of myself.

tactics Fri 22 Jan 2010

I think This? seems best. It's the same as it is now, but you can use null as a sentinel value.

kaushik Fri 22 Jan 2010

I will give the use case for both of this. We have built a small ORM for our project on top of fan sql.. So you can query like this

tag  := Tag{id=1}.one

Tag extends Entity which has the one() method. Simple and works great. There is also a list method that someone should call like this

tags := Tag{name="hello"}.list

Except now, I've to ask developers to use

Tag[] tags := Tag{name="hello"}.list

Also, one() has to throw exception now and not return null because This? is not allowed.

Login or Signup to reply.