且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

::(双冒号)代表什么?

更新时间:2022-05-13 03:25:38

您可以在Google上搜索 haskell双冒号" 或类似内容;不幸的是,要搜索语法有点困难,但是在这种情况下,您可以命名它.

You can google for haskell "double colon" or similar things; it's unfortunately a bit hard to google for syntax, but in this case you can name it.

在Haskell中,没有它,您的程序通常可以正常运行(尽管您将希望使用它来完善所定义的任何函数的规范,这是一种很好的做法).

In Haskell, your programs will often run fine without it (though you will want to use it to hone the specification of any functions you define, and it is good practice).

这个想法是,您可以在任何地方(甚至在表达式的中间)插入 :: ... 来表示通过Compiler先生的方式,该表达式应为 ... ".如果可以证明情况并非如此,则编译器将引发错误.

The idea is that you can insert a :: ... anywhere (even in the middle of an expression) to say "by the way Mr. Compiler, this expression should be of type ...". The compiler will then throw an error if it can be proved this may not be the case.

我认为您也可以使用它来将功能投射"到所需的版本;例如如果一个函数是多态的"(具有通用的类型签名),而您实际上想要一个 Integer ,那么您可以对结果值进行 :: Integer ;不过我有点生锈.

I think you can also use it to "cast" functions to the versions you want; e.g. if a function is "polymorphic" (has a general type signature) and you actually want, say an Integer, then you could do :: Integer on the resulting value perhaps; I'm a bit rusty though.