I got a couple questions after my first attempt with the new
typeclasses. First off, is it possible to call a function directly from
an instance? I tried this and it couldn't find the str2 functon:
///////////////////////////////////
#import <flx.flxh>
open List;
typeclass Str[T] {
virtual fun str2: T->string;
}
instance Str[int] {
fun str2 (x:int):string => str x;
}
print$ str2 5; endl;
///////////////////////////////////
It works if I replace the last line with this:
///////////////////////////////////
fun foo[T with Str[T]] (x:T):string => str2 x;
print$ foo 5; endl;
///////////////////////////////////
Second, typeclasses support recursive types, right? If so, how do I use
them? I couldn't get this section to compile:
///////////////////////////////////
open List;
fun foo[T with Str[T]] (x:list[T]):string =>
match x with
| Empty[T] => "()"
| Cons(?h, ?t) => "(" + (str2 h) + ", " + (foo x) + ")"
endmatch
;
///////////////////////////////////
It errors out with this:
Flx_exceptions.ClientError2(_, _, "Instantiate virtual function(2) str2")
index_4342 := foo[T with Str[T]]::x<3807>[<T3803>];
// begin match
// match case 1:|Empty[T]
if(not((match case 0)(index_4342[<T3803>])))goto _4340;
return "()";
// match case 2:|Cons (h, t)
_4340:
if(not((match case 1)(index_4342[<T3803>])))goto _4341;
return (String::add<2091> ((String::add<2091> ((String::add<2091>
((String::add<2091> ("(", (Str[T]::str2<3780>[<T3803>] ((arg of case 1
of index_4342[<T3803>])).mem_0))), ", ")), (foo<3802>[<T3803>] foo[T
with Str[T]]::x<3807>[<T3803>]))), ")"));
// match failure
_4341:
non_return_code c" FLX_MATCH_FAILURE("foo.flx",18,3,21,10);
"
Can't gen exes ..
CLIENT ERROR
Instantiate virtual function(2) str2
In foo.flx: line 20, cols 22 to 57
19: | Empty[T] => "()"
20: | Cons(?h, ?t) => "(" + (str2 h) + ", " + (foo x) + ")"
************************************
21: endmatch
See also foo.flx: line 6, cols 11 to 30
5: typeclass Str[T] {
6: virtual fun str2: T->string;
********************
7: }
-------------------------------------------------------------------------
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