F#:什么是有效的前缀运算符? [英] F#: What are the valid prefix operators?

查看:75
本文介绍了F#:什么是有效的前缀运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某处是否存在有效前缀运算符名称的列表?我找不到一个,但是我注意到有些过去在最后一个CTP中作为前缀运算符有效的运算符在RC中不再有效.

Is there a list of valid prefix operator names somewhere? I haven't been able to find one, but I've noticed that some operators that used to be valid as prefix operators in the last CTP are no longer valid in the RC.

let (~--) (str:string) = [str];;
-----^^^

stdin(4,6): error FS1208: Invalid operator definition. 
Prefix operator definitions must use a valid prefix operator name.

Brian的链接包括以下有效前缀运算符的列表:

Brian's link includes the following list of valid prefix operators:

! (or repetitions of !)
~ (or repetitions of ~)
+
-
+.
-.
%
%%
&
&&

下面的我的链接仅列出了这些运算符:

My link below lists only these operators:

~
!
?

一项快速测试表明,MSDN文档似乎与该语言规范不兼容.谢谢,布莱恩.

A quick test shows that the MSDN docs do not seem to be current with the language spec. Thanks, Brian.

推荐答案

请参见

http://research. microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html#_Toc245030784

阐明了规则.我在下面引用了其中的一些内容. (另请参见

which spells out the rules. I've quoted some of it below. (See also

http://msdn.microsoft.com/en -us/library/dd233228(VS.100).aspx

但是我认为它没有完整的规则.

but I think it doesn't have the full rules.)

以下符号操作标记可用于形成表达式:

The following symbolic-op tokens can be used to form expressions:

infix-op :=
    or || & && <OP >OP $OP = |OP &OP ^OP :: -OP +OP *OP /OP %OP 
    **OP

infix-or=prefix-op :=
    -OP +OP % %% & &&

prefix-op :=
    ! (or repetitions of !)
    ~ (or repetitions of ~) 
    +
    -
    +.
    -.
    %
    %%
    &
    &&

以下各项构成的运算符始终是前缀运算符:

Operators made of the following are always prefix operators:

  • 重复!
  • 重复〜

不允许使用以这些字符开头的其他运算符.

Other operators beginning with these characters are not permitted.

运算符+,-,+.,-.,%,&,&&并且可用作前缀和中缀运算符.当用作前缀运算符时,这些运算符的隐式运算符名称带有〜前缀.例如,-x被解析为表达式x的运算符〜-的应用.在为这些前缀运算符提供定义时,也会使用此名称:

The operators +, -, +., -., %, &, && and may be used as both prefix and infix operators. When used as prefix operators these operators have an implicit operator name with ~ prepended. For example, -x is parsed as an application of the operator ~- to the expression x. This name is also used when giving definitions for these prefix operators:

这意味着这些前缀运算符是通过添加〜字符来定义的:

This means that these prefix operators are defined with the ~ character added:

// For a complete redefinition of the operator:
let (~+) x = x

// For defining the operator on a type:
type C(n:int) =
    let n = n % 7
    member x.N = n
    static member (~+) (x:C) = x
    static member (~-) (x:C) = C(-n)
    static member (+) (x1:C,x2:C) = C(x1.N+x2.N)
    static member (-) (x1:C,x2:C) = C(x1.N-x2.N)

这篇关于F#:什么是有效的前缀运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆