关于`let 5 = 10` [英] About `let 5 = 10`

查看:113
本文介绍了关于`let 5 = 10`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我说 let 5 = 10 ,为什么 5 + 1 返回 6 代替 11

解决方案

你说

  let 5 = 10 

它不是重新定义5,它是一种模式匹配,当您说

  foo 5 = undefined 
... foo 10 ...

失败,如果它匹配。



在let表达式中,匹配是懒惰的。这意味着匹配只在被它所绑定的变量进行评估时完成。这使我们可以写下如下内容:

$ p $ let foo = undefined in 10

在你的表达式中,没有变量被绑定,所以该模式永远不会匹配。

可以说这样的模式没有变量在let-bindings中没有意义,应该由编译器检测,但该语言不禁止它们。

If I say let 5 = 10, why does 5 + 1 return 6 instead of 11?

解决方案

When you say

let 5 = 10

it's not a redefinition of 5, it's a pattern matching, the same which occurs when you say

foo 5 = undefined
 ... foo 10 ...

The pattern simply fails if it's ever matched.

In let-expressions the match is lazy. This means the match is only being done when a variable bound by it is evaluated. This allows us to write things like

 let foo = undefined in 10

In your expression, no variable is bound, so the pattern is never matched.

Arguably such patterns with no variables make no sense in let-bindings and should be detected by the compiler, but the language doesn't forbid them.

这篇关于关于`let 5 = 10`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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