In case you don't realise what this can mean here's an example:

Consider this:
 
        const stl_npos: size = "::std::string::npos";
        
        fun stl_find: string * string -> int = "(int)$1.find($2)" is cast;

        fun find (s:string, e:string) : opt[int] => 
                match stl_find (s, e) with 
                | ?i when i.size == stl_npos => None[int] 
                | ?i => Some i 
                endmatch
        ;

The first function is fast (in that it is a direct call of the C++) but
it is unsafe, because you might forget to check for stl_npos (std::string::npos 
in C++).

The second function is safe: you can't forget because the union "opt"
will not let you. But it is slow because it has to heap allocate its argument,
because the compiler doesn't know what an "int" is.

Here is code for a version of this which is safe AND fast:

        type pos = "int";
        fun _match_ctor_Npos: pos -> bool = "$1==npos";
        fun _match_ctor_Pos : pos -> bool = "$1!=npos";
        fun _ctor_arg_Pos: pos -> int = "(int)$1";

        fun stl_find: string * string -> Pos = "(int)$1.find($2)" is cast;

The only trick is to ensure we don't get lazy evaluation here!
We don't want you evaluate the find function 3 times!

But the compiler has already taken care of that as it happens:
a match argument is assigned to variable before doing the big
switch.

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to