Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static type variable, member constraint, compiler bug? ("Attempted to parse this as an operator name, but failed")

Tags:

inline

f#

f#-3.0

Compiles:

let inline f< ^T when ^T : (static member (<<<) : ^T * int -> ^T) > (x : ^T) = x <<< 1

Does not compile:

let inline f< ^T when ^T : (static member (>>>) : ^T * int -> ^T) > (x : ^T) = x >>> 1

Errors:

  1. Attempted to parse this as an operator name, but failed
  2. Unexpected symbol '>' in member signature. Expected ')' or other token.
  3. A type parameter is missing a constraint 'when ^T : (static member ( >>> ) : ^T * int32 -> ^T)'

Adding spaces doesn't help; this line yields the same compiler errors:

let inline f< ^T when ^T : (static member ( >>> ) : ^T * int -> ^T) > (x : ^T) = x >>> 1

I've searched both the documentation and the specification, to no avail. Is this a bug? Is there some way to include the > characters in the member signature?

like image 401
phoog Avatar asked Dec 08 '22 17:12

phoog


2 Answers

Sure looks like a bug. It's ugly, but one workaround is to use the long form of the operator name:

let inline f< ^T when ^T : (static member op_RightShift : ^T * int -> ^T)> (x : ^T) =
    x >>> 1
like image 127
kvb Avatar answered Jan 14 '23 07:01

kvb


Do you even need an explicit constraint? This works just as well:

let inline f (x: ^T) : ^T = x >>> 1
like image 22
Daniel Avatar answered Jan 14 '23 06:01

Daniel