November 20th Friday

本文探讨了Erlang编程语言中下划线变量的用途,包括标记不打算使用的变量以提高代码可读性及调试时避免编译警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Underscore Variables

 

  There’s one more thing to say about variables. The special syntax _VarName is used for a normal variable, not an anonymous variable. Normally the compiler will generate a warning if a variable is used only once in a clause since this is usually the sign of an error. If the variable is used only once but starts with an underscore, the warning message will not be generated.

 

  There are two main uses of underscore variables:

 

  • To name a variable that we don’t intend to use. That is, writing open(File, _Mode) makes the program more readable than writing open(File, _).
  • For debugging purposes. For example, suppose we write this:

          some_func(X) ->
             {P, Q} = some_other_func(X),
             io:format("Q = ~p~n", [Q]),
             P.

         This compiles without an error message. Now comment out the format statement:

         some_func(X) ->
            {P, Q} = some_other_func(X),
            %% io:format("Q = ~p~n", [Q]),
            P.

        If we compile this, the compiler will issue a warning that the variable Q is not used.

 

        If we rewrite the function like this:

        some_func(X) ->
           {P, _Q} = some_other_func(X),
           io:format("_Q = ~p~n", [_Q]),
           P.

       then we can comment out the format statement, and the compiler will not complain.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值