The following code now works and illustrates some good points
of typeclasses. It also shows a problem:

//////////////////////////////////////////
#import <flx.flxh>
include "stl";

open Reversible_Sequence[
  Stl::Vector::stl_vector[int], 
  Stl::Vector::stl_vector_iterator[int],
  Stl::Vector::stl_vector_reverse_iterator[int],
  int
];


var x: Stl::Vector::stl_vector[int];

print$ empty x; endl;

open Stl::Vector;
push_back  (x,4);
push_back  (x,5);
push_back  (x,6);

insert (x,begin x,3);
insert (x,end x,7);
insert (x,begin x,2);
insert (x,begin x,1);

print$ empty x; endl;

proc check[c,it,rit with Reversible_Sequence[c,it,rit,int]]
{
  // forward iterator: forwards
  var i = begin x;
  whilst i != end x do
    print$ *i; endl;
    i++;
  done;

  // forward iterator: backwards
  i = end x;
  whilst i != begin x do
    i--;
    print$ *i; endl;
  done;

  // reverse iterator: forwards
  var j = rbegin x;
  whilst j != rend x do
    print$ *j; endl;
    j++;
  done;

  // reverse iterator: backwards 
  j = rend x;
  whilst j != rbegin x do
    j--;
    print$ *j; endl;
  done;
}

check [
  Stl::Vector::stl_vector[int], 
  Stl::Vector::stl_vector_iterator[int],
  Stl::Vector::stl_vector_reverse_iterator[int],
  int
]();
//////////////////////////////////////////

What's cute is that the 'check' procedure is generic:
it will work on any Reversible_Sequence, not just vector.
The restriction to 'int' is simply because we don't have a Show[t]
typeclass for converting types to strings.

What isn't cute is the need to do this:

open Reversible_Sequence[
  Stl::Vector::stl_vector[int], 
  Stl::Vector::stl_vector_iterator[int],
  Stl::Vector::stl_vector_reverse_iterator[int],
  int
];

just so that

print$ empty x; endl;

works. What we wanted was this:

Reversible_Sequence[
  Stl::Vector::stl_vector[int], 
  Stl::Vector::stl_vector_iterator[int],
  Stl::Vector::stl_vector_reverse_iterator[int],
  int
]::empty x;

We can open a typeclass .. but we can't use a specialisation
of one as a qualifier in a qualified name. We can of course do:

fun empty[with Reversible_Sequence[
  Stl::Vector::stl_vector[int], 
  Stl::Vector::stl_vector_iterator[int],
  Stl::Vector::stl_vector_reverse_iterator[int],
  int
]] (x:stl_vector[int]) => empty x;

to work around this .. perhaps even with an anonymous lambda
(not sure if you can have polymorphic lambdas ..).

It seems absurd you can't access a typeclass function directly.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to