是否应该为主要指定类型签名?为什么/为什么不? [英] Should one specify a type signature for main or not? Why / why not?

查看:175
本文介绍了是否应该为主要指定类型签名?为什么/为什么不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从了解了解您对Haskell的好处第9章 $ b


按照惯例,我们通常不会为<$​​ c $ c> main 指定一个类型声明, 。

据我所知,这个约定很普遍。但是,如果我使用 -Wall 标志进行编译,该程序缺少 main 的类型签名,例如

- test.hs

- main :: IO ()
main = print(1 :: Int)

GHC会发出警告:

  $ ghc -Wall test.hs 
[1的1]编译主(test.hs,test.o )

test.hs:2:1:警告:
没有类型签名的顶级绑定:main :: IO()
链接测试...

我很困惑...如果 main的类型签名确实是多余的,为什么 -Wall 会导致GHC在丢失时抱怨?是否有很好的理由(除了摆脱那个警告)指定 main 的类型?

解决方案

好的,一般来说,正如那个警告明确指出的那样,给顶层绑定一个类型签名总是是一个好主意。事实上,比较合理的说法是,根据惯例,我们确实为所有东西指定了一个类型声明。 b


当然,在一个大项目中, main 本身构成了一项可忽略的工作,所以省略签名确实没有任何意义。然而,虽然Haskell对于结构合理的项目来说非常棒,但实际上有一种倾向,将几乎所有的东西都写入 / em>,作为一种快速脚本语言,对于其他人使用Python或Perl编写的东西来说,这也是一件非常好的事情。在这些情况下,你通常不太在乎安全性和良好的文档等,你只是想尽快写出尽可能简洁的文章来完成这项工作。您通常也不会使用 -Wall 编译这些脚本,而只需使用 runhaskell 执行它们。由于脚本始终需要包含 main (与大多数其他Haskell源文件不同),因此在此省略签名确实足够明智。



即使在最简单的情况下,我仍然怀疑现在的大多数Haskeller main :: IO()脚本,如果出于习惯。






1 只有顶部的所有内容水平,也就是说。本地签名有时也是有意义的,但它们通常会使代码混乱。


I learned from chapter 9 of Learn You A Haskell For Great Good that

By convention, we don't usually specify a type declaration for main.

As far as I can tell, this convention is widespread. However, if I compile, using the -Wall flag, a program that lacks a type signature for main, such as

-- test.hs

-- main :: IO ()
main = print (1 :: Int)

GHC does issue a warning:

$ ghc -Wall test.hs
[1 of 1] Compiling Main             ( test.hs, test.o )

test.hs:2:1: Warning:
    Top-level binding with no type signature: main :: IO ()
Linking test ...
$

I'm confused... If a type signature for main is indeed superfluous, why would -Wall cause GHC to complain when it's missing? Are there good reasons (aside from getting rid of that warning) for specifying main's type anyway?

解决方案

Well, generally speaking, as that warning makes clear, it's always a good idea to give top-level bindings a type signature. In fact, it would be more reasonable to say

By convention, we do specify a type declaration for everything1.

Certainly, in a big project, main itself makes up a neglectable effort, so it really doesn't make any sense to omit the signature. Just write it out, for sake of consistency.

However, though Haskell is great for properly structured projects and actually there's a tendency to write almost everything in libraries, it's also surprisingly good as a quick scripting language, for stuff other people would write in Python or Perl. And in those cases, you generally don't care that much about safety and good documentation etc., you just want to quickly write down something as concise as possible that does the job. You also normally don't compile those scripts with -Wall but just execute them with runhaskell. And as scripts always need to contain a main (unlike most other Haskell source files), it is indeed sensible enough to omit the signature here.

I'd still suspect that the majority of Haskellers nowadays do write main::IO() even in the simplest scripts, if just out of habit.


1Only everything on the top-level, that is. Local signatures sometimes do make sense as well, but often they rather clutter the code.

这篇关于是否应该为主要指定类型签名?为什么/为什么不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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