某处有有效前缀运算符名称列表吗?我找不到一个,但我注意到一些曾经在最后一个CTP中作为前缀运算符有效的运算符在RC中不再有效。
let (~--) (str:string) = [str];;
-----^^^
stdin(4,6): error FS1208: Invalid operator definition.
Prefix operator definitions must use a valid prefix operator name.
编辑:
Brian的链接包括以下有效前缀运算符列表:
! (or repetitions of !)
~ (or repetitions of ~)
+
-
+.
-.
%
%%
&
&&
下面的链接仅列出了这些运营商:
~
!
?
快速测试显示,MSDN文档似乎不符合语言规范。谢谢,Brian。
答案 0 :(得分:13)
见
http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html#_Toc245030784
阐明规则。我在下面引用了一些内容。 (另见
http://msdn.microsoft.com/en-us/library/dd233228(VS.100).aspx
但我认为它没有完整的规则。)
以下符号操作令牌可用于表达表达式:
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 ~)
+
-
+.
-.
%
%%
&
&&
由以下组成的运算符始终是前缀运算符:
不允许以这些字符开头的其他运算符。
运营商+, - ,+。, - 。,%,&amp;,&amp;&amp;并且可以用作前缀和中缀运算符。当用作前缀运算符时,这些运算符具有~prepended的隐式运算符名称。例如,-x被解析为运算符的应用程序〜 - 表达式x。在为这些前缀运算符提供定义时也使用此名称:
这意味着这些前缀运算符定义为添加了〜字符:
// 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)
答案 1 :(得分:13)
前缀运算符有两种类型:tilde-prefixed运算符和bang-prefixed运算符。 Tilde-prefixed运算符以波形符号“〜”开头,而bang-prefixed运算符以“!”开头。主要区别在于,在应用波形前缀运算符时省略了波形符。
波形符前缀运算符以波形符号“〜”开头,后跟其中一个:
+, - ,+。, - 。,%,%%,&amp;,&amp;&amp;
波浪号前缀运算符也可以构建为任意数量波段的列表:
~~,~~~,~~~~,~~~~~,...
当您应用操作符时,您必须省略波浪号,除非它是波浪蛇。
╔═════════════╦════════════╦══════════════════════════╗
║ declaration ║ usage ║ remark ║
╠═════════════╬════════════╬══════════════════════════╣
║ ~~ ║ ~~ ║ ║
║ ~~~ ║ ~~~ ║ ║
║ ~~~~~~~~~ ║ ~~~~~~~~~ ║ or any number of tildes ║
║ ~+ ║ + ║ ║
║ ~+. ║ +. ║ ║
║ ~- ║ - ║ ║
║ ~-. ║ -. ║ ║
║ ~% ║ % ║ ║
║ ~%% ║ %% ║ ║
║ ~& ║ & ║ ║
║ ~&& ║ && ║ ║
╚═════════════╩════════════╩══════════════════════════╝
一个爆炸前缀的操作员以一声巨响“!”开头。其次是这些字符的任意组合:
!,%,&amp;,*,+,。,/,&lt;,=,&gt;,@,^,|,〜,?
此规则的例外是“!=”。前缀运算符不得以此组合开头。
此表列出了一些示例。
╔══════════════╦══════════════╗
║ declaration ║ usage ║
╠══════════════╬══════════════╣
║ !! ║ !! ║
║ !% ║ !% ║
║ !& ║ !& ║
║ !* ║ !* ║
║ !+ ║ !+ ║
║ !. ║ !. ║
║ !/ ║ !/ ║
║ !< ║ !< ║
║ !> ║ !> ║
║ !@ ║ !@ ║
║ !^ ║ !^ ║
║ !| ║ !| ║
║ !~ ║ !~ ║
║ !? ║ !? ║
║ !@=@> ║ !@=@> ║
║ !!!/<=>@!!!* ║ !!!/<=>@!!!* ║
╚══════════════╩══════════════╝
type SomeString = SomeString of string
let ( ~~ ) (SomeString s) = "~~ " + s
let ( ~~~ ) (SomeString s) = "~~~ " + s
let ( ~~~~~~~~~ ) (SomeString s) = "~~~~~~~~~ " + s
let ( ~+ ) (SomeString s) = "~+ " + s
let ( ~+. ) (SomeString s) = "~+. " + s
let ( ~- ) (SomeString s) = "~- " + s
let ( ~-. ) (SomeString s) = "~-. " + s
let ( ~% ) (SomeString s) = "~% " + s
let ( ~%% ) (SomeString s) = "~%% " + s
let ( ~& ) (SomeString s) = "~& " + s
let ( ~&& ) (SomeString s) = "~&& " + s
let x = SomeString "tilde"
printf "%s\n" (~~ x) // ~~ tilde
printf "%s\n" (~~~ x) // ~~~ tilde
printf "%s\n" (~~~~~~~~~ x) // ~~~~~~~~~ tilde
printf "%s\n" (+ x) // ~+ tilde
printf "%s\n" (+. x) // ~+. tilde
printf "%s\n" (- x) // ~- tilde
printf "%s\n" (-. x) // ~-. tilde
printf "%s\n" (% x) // ~% tilde
printf "%s\n" (%% x) // ~%% tilde
printf "%s\n" (& x) // ~& tilde
printf "%s\n" (&& x) // ~&& tilde
type SomeString = SomeString of string
let ( !! ) (SomeString s) = "!! " + s
let ( !% ) (SomeString s) = "!% " + s
let ( !& ) (SomeString s) = "!& " + s
let ( !* ) (SomeString s) = "!* " + s
let ( !+ ) (SomeString s) = "!+ " + s
let ( !. ) (SomeString s) = "!. " + s
let ( !/ ) (SomeString s) = "!/ " + s
let ( !< ) (SomeString s) = "!< " + s
let ( !> ) (SomeString s) = "!> " + s
let ( !@ ) (SomeString s) = "!@ " + s
let ( !^ ) (SomeString s) = "!^ " + s
let ( !| ) (SomeString s) = "!| " + s
let ( !~ ) (SomeString s) = "!~ " + s
let ( !? ) (SomeString s) = "!? " + s
let ( !@=@> ) (SomeString s) = "@=@> " + s
let ( !!!/<=>@!!!* ) (SomeString s) = "!!!/<=>@!!!* " + s
//let ( != ) (SomeString s) = "!= " + s // prefix != is not allowed
//let ( !=@@ ) (SomeString s) = "!= " + s // prefix != is not allowed
let y = SomeString "bang"
printf "%s\n" (!! y) // !! bang
printf "%s\n" (!% y) // !% bang
printf "%s\n" (!& y) // !& bang
printf "%s\n" (!* y) // !* bang
printf "%s\n" (!+ y) // !+ bang
printf "%s\n" (!. y) // !. bang
printf "%s\n" (!/ y) // !/ bang
printf "%s\n" (!< y) // !< bang
printf "%s\n" (!> y) // !> bang
printf "%s\n" (!@ y) // !@ bang
printf "%s\n" (!^ y) // !^ bang
printf "%s\n" (!| y) // !| bang
printf "%s\n" (!~ y) // !~ bang
printf "%s\n" (!? y) // !? bang
printf "%s\n" (!@=@> y) // !@=@> bang
printf "%s\n" (!!!/<=>@!!!* y) // !!!/<=>@!!!* bang
答案 2 :(得分:0)
没关系,我想我found the list here,在“重载运营商名称”标题下的第二个表格中。