开发者

How do I see the type for operators in F# interactive?

I am trying to explore the type开发者_开发知识库 of operators such as :: in F# interactive.

But I get these kinds of messages:

Unexpected symbol '::' in expression. Expected ')' or other token.

even if I surround it with (::).


I do it like this:

> let inline showmecons a b = a :: b;;

val inline showmecons : 'a -> 'a list -> 'a list

or

> let inline showmepow a b = a ** b;;

val inline showmepow :
   ^a ->  ^b ->  ^a when  ^a : (static member Pow :  ^a *  ^b ->  ^a)


You'll see the type of usual operators if you surround them with parentheses:

> (+);;
val it : (int -> int -> int) = <fun:it@4-5>

Unfortunatelly, this restricts the type of the operator to one specific type - F# Interactive doesn't print the polymorphic definition (with constraints). You can use the workaround suggested by Stephen (and define a new inline function) to see that.

The reason why it doesn't work for :: is that :: is actually a special syntactic construct (defined directly in the F# specification).


This is pretty old, but I'm learning F# and also wanted to figure this out.

Looking at the F# specification on page 32, we see that symbolic keywords also have a compiled name in F#. The equivalent compiled name for :: is op_ColonColon, which actually accepts tuples:

> op_ColonColon;;
val it : arg0:'a * arg1:'a list -> 'a list = <fun:clo@22-5>`

Using :: to define an inline function will give us a curried cons function, which is misleading, I believe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜