单态限制,ghci,并让表达 [英] Monomorphism Restriction, ghci, and let expression

查看:84
本文介绍了单态限制,ghci,并让表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  f = show 

然而,在 ghci 中,这是合法的

  let f = show 

此外,它的类型改为

 () - >字符串

有没有这种现象的解释?

解决方案

ghci 提示的行为如同 ExtendedDefaultRules 扩展程序已启用。



<特别是这意味着:

lockquote
单元类型()被添加到标准类型列表的开始处
这是在执行类型默认时尝试的。


因此,要从源文件中获取相同的行为,可以使用 -XExtendedDefaultRules ,或者将 { - #LANGUAGE ExtendedDefaultRules# - } 添加到文件顶部。


This expression is incorrect.

f = show

However, in ghci this is legit

let f = show

Moreover, its type is changed to

() -> String

Is there any explanation of this phenomenon?

解决方案

The ghci prompt behaves as if the ExtendedDefaultRules extension is enabled.

In particular this means that:

The unit type () is added to the start of the standard list of types which are tried when doing type defaulting.

So to get the same behaviour from a source file, either compile with -XExtendedDefaultRules, or add {-# LANGUAGE ExtendedDefaultRules #-} to the top of the file.

这篇关于单态限制,ghci,并让表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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