let 5 = 10 有什么作用?不是赋值操作吗? [英] What does let 5 = 10 do? Is it not an assignment operation?

查看:24
本文介绍了let 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.

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

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