Thursday, March 5, 2009

F# interface implementation - is `unit` special?

#light

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

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

2 comments:

hsenag said...

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).

Anton Tayanovskyy (toyvo) said...

Thanks, hsenag! The workaround makes sense.

Post a Comment