F# Static Member Type Constraints

Posted by Stephen Swensen on Stack Overflow See other posts from Stack Overflow or by Stephen Swensen
Published on 2010-05-15T15:51:13Z Indexed on 2010/05/15 15:54 UTC
Read the original article Hit count: 574

Filed under:
|

I'm trying to define a function, factorize, which uses structural type constraints (requires static members Zero, One, +, and /) similar to Seq.sum so that it can be used with int, long, bigint, etc. I can't seem to get the syntax right, and can't find a lot of resources on the subject. This is what I have, please help.

let inline factorize (n:^NUM) =
    ^NUM : (static member get_Zero: unit->(^NUM))
    ^NUM : (static member get_One: unit->(^NUM))
    let rec factorize (n:^NUM) (j:^NUM) (flist: ^NUM list) = 
        if n = ^NUM.One then flist
        elif n % j = ^NUM.Zero then factorize (n/j) (^NUM.One + ^NUM.One) (j::flist)
        else factorize n (j + ^NUM.One) (flist)
    factorize n (^NUM.One + ^NUM.One) []

© Stack Overflow or respective owner

Related posts about F#

Related posts about functional-programming