简体   繁体   中英

Nested generic constraints in f#

I'm trying to define an extension method on a generic class, ( Xceed.Wpf.Toolkit.NumericUpDown ), but restrict that generic to Nullable<_> , however I can't figure out the syntax. The essence of what I want is

type NumericUpDown<Nullable<_>> with
  member x.getVal() = x.Value.GetValueOrDefault()
  member x.setVal v = x.Value <- Nullable v

but that won't compile. I've tried several variations on that theme, but nothing works. Is there a way to do this?

I don't know the class you want to extend but as from F# 3.1 you should be able to write and consume this:

open System.Runtime.CompilerServices

[<Extension>]
type NumericUpDownExtensions () =
  [<Extension>]
  static member getVal(x: NumericUpDown<Nullable<'a>>) = x.Value.GetValueOrDefault()
  [<Extension>]
  static member setVal(x: NumericUpDown<Nullable<'a>>, v) = x.Value <- Nullable v

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM