Loading ...
Sorry, an error occurred while loading the content.

Passing polymorphic function as a parameter

Expand Messages
  • Dmitry Bely
    The following does not type check: let pair_op (f: a- a- a) ((int1:int),(float1:float)) ((int2:int),(float2:float)) = (f int1 int2),(f float1 float2) let
    Message 1 of 2 , Apr 10, 2009
    • 0 Attachment
      The following does not type check:

      let pair_op (f:'a->'a->'a) ((int1:int),(float1:float))
      ((int2:int),(float2:float)) =
      (f int1 int2),(f float1 float2)

      let min_pair = pair_op min
      let max_pair = pair_op max

      Error: This expression has type float but is here used with type int

      Is there a way to convince type checker that f is polymorphic?

      - Dmitry Bely
    • Lukasz Stafiniak
      No, you have to pack it into a record {f : a. a- a- a} and unpack it twice: let pair_op op .... = op.f int1 int2, op.f float1 float2
      Message 2 of 2 , Apr 10, 2009
      • 0 Attachment
        No, you have to pack it into a record {f : 'a.'a->'a->'a} and unpack it twice:
        let pair_op op .... =
        op.f int1 int2, op.f float1 float2

        On Fri, Apr 10, 2009 at 4:59 PM, Dmitry Bely <dmitry.bely@...> wrote:
        >
        >
        > The following does not type check:
        >
        > let pair_op (f:'a->'a->'a) ((int1:int),(float1:float))
        > ((int2:int),(float2:float)) =
        > (f int1 int2),(f float1 float2)
        >
        > let min_pair = pair_op min
        > let max_pair = pair_op max
        >
        > Error: This expression has type float but is here used with type int
        >
        > Is there a way to convince type checker that f is polymorphic?
        >
        > - Dmitry Bely
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.