Thursday, March 5, 2009

F# interface implementation - is `unit` special?


type 'a A = 
    abstract member A : unit -> 'a

let x = { new int  A with member x.A() = 1 }  // ALLOWED
let y = { new unit A with member x.A() = () } // ERROR?


  1. Yes, unit return types are special (they translate to void) and interact badly with generic code like you have above. We (Credit Suisse) discovered this and reported it to the F team a few weeks ago, but there's no promise of a direct solution because unit return type -> void is an important performance guarantee. The workaround, if I recall correctly, is to use brackets to turn A into a value rather than a function: abstract member A : (unit -> 'a).

  2. Thanks, hsenag! The workaround makes sense.