“让5 = 10"是做什么的?这不是分配操作吗? [英] What does `let 5 = 10` do? Is it not an assignment operation?

查看:117
本文介绍了“让5 = 10"是做什么的?这不是分配操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我说let 5 = 10,为什么5 + 1返回6而不是11?

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

推荐答案

当你说

let 5 = 10

它不是5的重定义,是模式匹配,与您说的相同

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.

在let表达式中,匹配是惰性的.这意味着仅在评估由其绑定的变量时才进行匹配.这样我们就可以写类似

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.

可以说,没有变量的这种模式在let绑定中没有意义,应该由编译器检测,但是语言并不禁止它们.

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.

这篇关于“让5 = 10"是做什么的?这不是分配操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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