On Thu, 2006-11-16 at 10:09 -0800, Erick Tryzelaar wrote:
> Assuming I had a typeclass like this:
>
> typeclass Show[t] {
> virtual fun show: t -> string;
> }
>
> How can I define a function to convert a tuple? I'm not sure how I
> specify that it uses two typeclasses. This is what I tried and got a
> parse error:
>
> fun show[T,U with Show[T], Show[U]] ((x,y):T*U):string =>
> '(' + (show x) + ', ' + (show y) + ')'
> ;
There are several ways, this looks the easiest:
////////////////////////////////////
#import <flx.flxh>
typeclass Show[t] {
virtual fun show: t -> string;
}
fun show[T,U with Show[T], Show[U]] (xy:T*U):string =>
let ?x,?y = xy in
'(' + (show x) + ', ' + (show y) + ')'
;
///////////////////////////////////////
Unlike Ocaml .. unfortunately .. Felix doesn't pattern match
in parameter lists, so you can't have parameters like
(x,y)
other than at the top level -- in this trivial case you could
have done:
fun show[T,U with Show[T], Show[U]] (x:T,y:U):string => ...
since it happens the argument is a tuple.
--
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