为什么我不应该混合制表符和空格? [英] Why shouldn't I mix tabs and spaces?

查看:613
本文介绍了为什么我不应该混合制表符和空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常读到,我不应该在Haskell中混合制表符和空格,或者我不应该使用制表符。为什么?

I often read that I shouldn't mix tabs and spaces in Haskell, or that I shouldn't use tabs at all. Why?

推荐答案

问题是双重的。首先,Haskell对缩进敏感,例如以下代码无效:

The problem is twofold. First of all, Haskell is indentation sensitive, e.g. the following code isn't valid:

example = (a, b)
  where
    a = "Hello"
     b = "World"

这两个绑定都需要使用相同数量的缩进空格/制表符(请参阅关闭规则)。虽然在这种情况下很明显,但它隐藏在下面的一个中,其中我用·表示一个空格,并用»

Both bindings need to be indented with the same number of spaces/tabs (see off-side rule). While it's obvious in this case, it's rather hidden in the following one, where I denote a space by · and a tab by »:

example = (a, b)
··where
····a = "Hello"
»   b = "World"

这看起来像是有效的Haskell如果编辑器将显示与4倍对齐的选项卡,则编码。但事实并非如此。 Haskell选项卡以8的倍数对齐,所以代码将如下解释:

This will look like valid Haskell code if the editor will show tabs aligned to multiples by four. But it isn't. Haskell tabs are aligned by multiples of eight, so the code will be interpreted like this:

example = (a, b)
··where
····a = "Hello"
»       b = "World"

其次,如果您仅使用选项卡,则最终可能会出现看起来不正确的布局。例如,如果选项卡显示六个或更多空格(本例中为八个),则以下代码看起来正确:

Second, if you use only tabs, you can end up with a layout that doesn't look right. For example, the following code looks correct if a tab gets displayed with six or more spaces (eight in this case):

example = (a, b)
»       where»  a = "Hello"
»       »       b = "World"

但是在另一个使用4个空格的编辑器中,它看起来不再正确:

But in another editor that uses 4 spaces it won't look right anymore:

example = (a, b)
»   where»  a = "Hello"
»   »   b = "World"

但它仍然正确。但是,习惯了空格的人可能会重新使用空格绑定 b '并最终出现解析器错误。

It's still correct, though. However, someone who's used to spaces might reindent b' binding with spaces and end up with a parser error.

如果您在整个代码中强制执行代码约定,以确保只在行首使用制表符,并在之后使用换行符,其中 do 您可以避免一些问题(请参阅 11 )。但是,当前版本的 GHC默认警告标签,因为他们 been a 过去很多解析器错误,所以您可能也想摆脱它们。

If you enforce a code convention throughout your code that makes sure that you only use tabs at the beginning of a line and use a newline after where, let or do you can avoid some of the problems (see 11). However, current releases of GHC warn about tabs by default, because they have been a source of many parser errors in the past, so you probably want to get rid of them too.


  • 关于主题的重复线程(大多数职业空间,但一些职业选项卡) .earth.li /〜ian / style / haskell.htmlrel =noreferrer>良好的Haskell风格(pro spaces)
  • 另一个选项卡v空间辩论(专业混合)
  • A reddit thread on the topic (majority pro spaces, but some pro tabs)
  • Good Haskell Style (pro spaces)
  • Yet Another Tabs v Space debate (pro mixing)

这篇关于为什么我不应该混合制表符和空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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