为什么未使用此变量? [英] Why is this variable unused?

查看:60
本文介绍了为什么未使用此变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要编译此代码:

triples( [], _,_,_)->
  [];

triples( Self, X, Y, none )->
  [ Result || Result = { X, Y, _} <- Self ].

报告:

./simple_graph.erl:63: Warning: variable 'X' is unused
./simple_graph.erl:63: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'X' is unused
./simple_graph.erl:64: Warning: variable 'X' shadowed in generate
./simple_graph.erl:64: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'Y' shadowed in generate

并返回错误的结果:完整的自我.

And return wrong result: full Self.

推荐答案

这是因为生成器的LHS上的变量X和Y始终是理解的新的未绑定变量 local .这意味着它们与三元组开头的X和Y变量不同,因此,没有隐式相等性检验.这类似于乐趣,其中出现在乐趣开头的所有变量都是该乐趣本地的新变量.

This is because variables occurring on the LHS of generators, X and Y here, are always new unbound variables local to the comprehension. This means that they are not the same variables as the X and Y in the head of triples and, therefore, there is no implicit equality test. This similar to funs where all variables occurring in the head of a fun are alse new variables local to the fun.

这与大多数erlang的其他地方不同,这就是为什么编译器不仅警告未使用头部的X和Y,而且警告理解中的X和Y遮盖了其他变量.它们在理解中的任何地方也都未使用.

This is different from most of the rest of erlang, which is why the compiler not only warns that the X and Y in the head are not used but also that the X and Y in the comprehension shadow the other variables. They are also unused anywhere in the comprehension.

一种简单的方法来获得您想要的东西:

An easy way to get what you want is:

[ Result || Result = {X1,Y1,_} <- Self, X =:= X1, Y =:= Y1 ]

这篇关于为什么未使用此变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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