Question

I'm trying to make a record with discriminated unions as members, but after making the "API" record, I only have access to the unions and not what I put in them....

type DefaultExpr = Expr<int -> Ref<int> -> int -> unit>
type Option1Expr = Expr<int -> Ref<int> -> int -> Ref<int> -> unit>
type Option2Expr = Expr<int -> Ref<int> -> int -> Ref<int> -> Ref<int> -> unit>

module Identityless =
    type DefaultExpr = Expr<int -> Ref<int> -> unit>
    type Option1Expr = Expr<int -> Ref<int> -> Ref<int> -> unit>
    type Option2Expr = Expr<int -> Ref<int> -> Ref<int> -> Ref<int> -> unit>

type DefaultU =
    | ID of DefaultExpr
    | NoID of Identityless.DefaultExpr

type Option1U =
    | ID of Option1Expr
    | NoID of Identityless.Option1Expr

type Option2U =
    | ID of Option2Expr
    | NoID of Identityless.Option2Expr


type API =
    {
        Default :   DefaultU
        Option1 :   Option1U
        Option2 :   Option2U
    }


module InclusiveSum =

    let private Default (scan_op:IScanOp) =
        <@ fun (input:int) (output:Ref<int>) -> () @>

    let private Option1 (scan_op:IScanOp) =
        <@ fun (input:int) (output:Ref<int>) (block_aggregate:Ref<int>) -> () @>

    let private Option2 (scan_op:IScanOp) =
        <@ fun (input:int) (output:Ref<int>) (block_aggregate:Ref<int>) (block_prefix_callback_op:Ref<int>) -> () @>


    let api scan_op =
        {
            Default = scan_op |> Default |> DefaultU.NoID                       
            Option1 = scan_op |> Option1 |> Option1U.NoID                 
            Option2 = scan_op |> Option2 |> Option2U.NoID
        }

So when I make my api...

let foo = InclusiveSum.api (scan_op ADD 0)

I want foo.Default to give me my DefaultExpr to use

After reading through some posts, I think I would have to go back and do some pattern matching which would kind of defeat the whole purpose here...

So how do I get my DefaultExpr back out of the API record?

Was it helpful?

Solution

Since the type of API.Default is the discriminated union type Default1U, the only(*) way to work with the value inside API.Default is by pattern matching. So you'll need something like:

match foo with 
  ID e -> ...
| NoId e -> ...

There is no other way(*).

(*) Ignoring reflection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top