Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface ReversableList<T> {
- // Return a copy of this list but reversed
- ReversableList<T> Reverse();
- // We still need to return the contained type sometimes
- T Get(int index)
- }
- public class ListImplementation<T> : ReversableList<T>
- ReversableList<T> Reverse();
- }
- var concreteList = new ListImplementation<String>();
- ReversableList<T> reversed = concreteList.Reverse();
- var concreteList = new ListImplementation<String>();
- ListImplementation<String> reversed = concreteList.Reverse();
- trait GenSeqLike[+A, +Repr] ...
- def reverse: Repr // <--- NO BODY, means abstract
- trait SeqLike[+A, +Repr] extends GenSeqLike[A, Repr]
- def reverse: Repr
- //iterates through this seqence
- // and reverses
- for (x <- this)
- //...etc
- trait Seq[+A] extends SeqLike[A, Seq[A]] //<-- NOTE here is the type for "Repr"
- //no mention of reverse
- trait LinearSeq[+A] extends Seq[A] with SeqLike[A, Seq[A]] //<-- NOTE type again
- // no mention of reverse
- sealed abstract class List[+A] extends LinearSeq[A]
- with LinearSeqOptimized[A, List[A]]
- override def reverse: List[A] = {
- //faster impl without foreach iteration
- ...
- }
- public interface ReversableList<L, T> where L : ReversableList<L, T>
- {
- // Return a copy of this list but reversed
- L Reverse();
- }
- public class ListImplementation<T> : ReversableList<ListImplementation<T>, T>
- {
- public ListImplementation<T> Reverse() { /* code */ }
- }
- public interface IReversableList<T> where T : IReversableList<T>
- {
- // Return a copy of this list but reversed
- T Reverse();
- }
- public class ListImplementation<T> : IReversableList<ListImplementation<T>>
- {
- public ListImplementation<T> Reverse()
- {
- return new ListImplementation<T>();
- }
- }
- var concreteList = new ListImplementation<string>();
- ListImplementation<string> reversed = concreteList.Reverse();
- var list = new List<string>(){"a","b","c"};
- list.Reverse();
Add Comment
Please, Sign In to add comment