Listable class: accessing a class as if it were a list

48 views
Skip to first unread message

Gonzalo Chumillas

unread,
Jun 21, 2016, 3:44:35 PM6/21/16
to mi...@dartlang.org
Is there any way to access an instance of a class as if it were a List. I know that when a class extends the IterableBase class, it can be accessed as if it were a collection of values. What I mean:

// ListableBase does not exist, of course
class MyArrayLikeClass extends ListableBase<int> {
   
// some code here
}


var myVar = new MyArrayLikeClass();
print(myVar[10]);

Bob Nystrom

unread,
Jun 21, 2016, 4:31:41 PM6/21/16
to General Dart Discussion
ListBase<T> in dart:collection is what you want. :)

Cheers!

– bob


On Tue, Jun 21, 2016 at 12:44 PM, Gonzalo Chumillas <gonzalo....@gmail.com> wrote:
Is there any way to access an instance of a class as if it were a List. I know that when a class extends the IterableBase class, it can be accessed as if it were a collection of values. For example:

// ListableBase does not exist, of course
class MyArrayLikeClass extends ListableBase<int> {
   
// some code here
}


var myVar = new MyArrayLikeClass();
print(myVar[10]);

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Lasse R.H. Nielsen

unread,
Jun 21, 2016, 4:32:23 PM6/21/16
to mi...@dartlang.org
I'm not sure exactly what you want to achieve. If you just want the index operator, you can implement just that:

   class MyArrayLikeClass {
     operator[](int index) => returnSomeValue(basedOn: index);
     ...
   }

If you want the full List behavior, you can extend ListBase from dart:collection (https://api.dartlang.org/stable/1.17.1/dart-collection/ListBase-class.html)

   class MyArrayLikeClass<T> extends ListBase<T> {
      T operator(int index) => ...;
      int get length => ...
      ...
   }

In that case you have to implement a number of methods (documented in ListBase).

/L

On Tue, Jun 21, 2016 at 9:44 PM, Gonzalo Chumillas <gonzalo....@gmail.com> wrote:
Is there any way to access an instance of a class as if it were a List. I know that when a class extends the IterableBase class, it can be accessed as if it were a collection of values. For example:

// ListableBase does not exist, of course
class MyArrayLikeClass extends ListableBase<int> {
   
// some code here
}


var myVar = new MyArrayLikeClass();
print(myVar[10]);

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Gonzalo Chumillas

unread,
Jun 22, 2016, 7:49:22 AM6/22/16
to Dart Misc
Thanks. That's what I was looking for.
Reply all
Reply to author
Forward
0 new messages