❮❮❮ ❮❮❮   ❯❯❯ ❯❯❯
Using Gurobi operators from F#
Problems with referencing C# defined custom operator from F#.

Tags: fs gurobi
Reading time: 1 min.

Dear community,

I have a problem and my preferred solution does not work:

1. Problem

There is a C# library with an overloaded == operator gurobi 9.1 api doc. I want to use this operator in F#, such as in

  let setEqual (lhs : GRBLinExpr, rhs : GRBLinExpr) =
      GRBLinExpr.(==) (lhs, rhs)

This fails with “FS0039 The type ‘GRBLinExpr’ does not define the field, constructor or member ‘==’.”

Also addressing it as member op_EqualsEquals as outlined in F# oprator docs leads to the same error.

2. Extra information

Adding the library to a C# project shows me:

public static Gurobi.GRBTempConstr operator ==(Gurobi.GRBLinExpr lhs, Gurobi.GRBLinExpr rhs)
    Member of Gurobi.GRBLinExpr

and the following compiles

static GRBTempConstr SetEqual(GRBLinExpr lhs, GRBLinExpr rhs)
    {
      return lhs == rhs;
    }

Possible workaround

I can create an extra C# project, wrap the == operator there in a static method and reuse this method in F#. Pro: Works. Cons: extra project.

Questions

a. Why does F# not allow me to use this operator? Are there some limitations to using operators from F#? The F# oprator docs do not mention any.

b. Are there other workarounds? Maybe with reflection?

Solution

Easier than thought: C# uses ==, F# uses = and I forgot that.

Refs