0

Is it possible to do a member overload with generic constraints? Here, I am trying to create and AddWithOption method to support both value types and reference types. If you look at the gist, I show several attempts to make this happen.

enter image description here

My workaround was simply to not overload the member. I renamed with one for value types to AddWithOptionValue. However, it would be cool if the overload worked. Any ideas?

Cameron Taggart
  • 5,771
  • 4
  • 45
  • 70
  • Also not the best code example. A better implementation is: `member x.AddWithOption (nm, op:Option<_>) = if op.IsSome then x.AddWithValue (nm, op.Value) else x.AddWithValue (nm, DBNull.Value)` – Cameron Taggart Dec 11 '14 at 22:38

1 Answers1

9

Constraints are not part of method signature (per ECMA 335, I.8.6.1.5 Method signatures) meaning that in CIL you cannot have two methods which signatures differ only in constraints. In order to encode such signatures in CIL one need to use optional or required modifiers but this is not very trivial and today F# does not do that.

desco
  • 16,642
  • 1
  • 45
  • 56
  • 3
    Thanks! Makes sense. I read more on [Why aren't type constraints part of the method signature?](http://stackoverflow.com/questions/9440888/why-arent-type-constraints-part-of-the-method-signature) and it helped. – Cameron Taggart Dec 09 '14 at 20:32