On Wed, 2007-11-28 at 10:55 -0800, Erick Tryzelaar wrote:
> On Nov 28, 2007 8:44 AM, skaller <[EMAIL PROTECTED]> wrote:
> > I wanted to test complex (and float) functions, with code like:
> > The 'right' solution is to use, for example:
> >
> > str_float (x, (width=10, prec=5, fmt=fixed))
> >
> > [where the second argument is one of those new fangled
> > record things ..] Or something similar of course .. :)
>
> I like that :)
Ok, we now have:
fmt[dcomplex,double](z,default (5,2)))
which formats a double precision complex number. The problem
is that unlike the float case, you have to say dcomplex,double.
The function is actually:
fun fmt[t,r with Complex[t,r]] (v:t, m: mode) =>
but unfortunately the r (underlying real type) cannot be
deduced, even though it is determined by the complex type.
The 'r' is in the typeclass because for example:
virtual fun imag: t -> r;
needs it. It is easy to write a type function connecting t and r,
indeed there is already a typedef:
typedef complex[t in floats] = typematch t with
| float => fcomplex
| double => dcomplex
| ldouble => lcomplex
endmatch
;
However this cannot be used in a typeclass, because the
constraint (of t to floats) cannot be represented:
typeclass Complex[r] {
typedef t = complex[r];
inherit Eq[t];
...
This would work I think if the function were parametric, eg
typedef t = r * r;
is total and constructive, so should be fine. But typematches
can't work on type variables.
Note this problem doesn't arise for floats:
fun fmt[t in reals] (v:t, m: mode) =>
match m with
| default (?w,?p) => fmt_default(v,w,p)
| fixed (?w,?p) => fmt_fixed(v,w,p)
| scientific(?w,?p) => fmt_scientific(v,w,p)
endmatch
;
There's no typeclass here. I could solve the problem for complex
easily, by writing a routine in C++ for the complex values,
but it cannot be done in Felix: the actual code:
fun fmt[t,r with Complex[t,r]] (v:t, m: mode) =>
match m with
| default (?w,?p) =>
fmt_default(real v,w,p) +"+"+fmt_default(imag v,w,p)+"i"
| fixed (?w,?p) =>
fmt_fixed(real v,w,p)+"+"+fmt_fixed(imag v,w,p)+"i"
| scientific(?w,?p) =>
fmt_scientific(real v,w,p)+"+"+fmt_scientific(imag v,w,p)+"i"
endmatch
;
delegates to compositions of the fmt over the reals.
[Note: the code does work .. you just have put in the [dcomplex,double]:
fmt[dcomplex,double](z,default (5,2)))
which is very unfortunate]
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell. From the desktop to the data center, Linux is going
mainstream. Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language