为什么F#具有一元加号运算符? [英] Why does F# have a unary plus operator?

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

问题描述

某些语言使用一元加号运算符进行隐式转换,例如将字符串强制转换为数字(例如Javascript)或将较小的数字类型转换为int(例如大多数基于C的语言),或者在以下情况下使用重载运算符.

Some languages use a unary plus operator for implicit conversions, such as coercing a string to a number (e.g. Javascript) or casting small number types to an int (e.g. most C-based languages), or to be used when overloading operators.

由于一元加号主要用于这样的骇客目的,并且由于F#不执行自动加宽转换,所以令我惊讶的是F#包含一元加号.

Since the unary plus is primarily used for hackish purposes like this, and also since F# does not perform automatic widening conversions, I was surprised that F# includes the unary plus.

让我惊讶的是,Haskell没有一元加运算符.由于F#设计受Haskell的影响,我很好奇为什么为什么在Haskell显然不需要F#的情况下才决定F#需要一元加号.

What adds to my surprise is that Haskell does not have a unary plus operator. Since the F# design was influenced by Haskell, I'm curious as to why it was decided that F# needed a unary plus when Haskell apparently didn't.

您能举一个F#中一元加号可靠使用的例子吗?如果不能,为什么它完全包含在语言中?

Can you give an example of a credible use for the unary plus in F#? If you can't, why is it included in the language at all?

推荐答案

我将总结扩展注释.可能的原因(直到给出更权威的答案):

I'll summarize the extended comments. Possible reasons (until a more authoritative answer is given):

  1. 与OCaml的一致性,从中衍生出F#.(如果您做错了/不必要的话,最好继续这样做,以便人们知道会发生什么:-))
  2. 重载(主要用于自定义类型)
  3. 一元否定的对称性

这篇关于为什么F#具有一元加号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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