3

I've searched for an answer here and elsewhere online, but all topics deal with either iterating a table, metatables, or times when _,var1 = do_some_stuff() which are not the case here.

This is just a non-realistic function, but containing examples of what I mean:

function do_some_stuff(data)
    _ = some_function(data)
    some_other_code
    _ = some_other_function(data)
end

Wouldn't this be considered the same as simply entering:

function do_some_stuff(data)
    some_function(data)
    some_other_code
    some_other_function(data)
end

I know that if I create a basic Lua program like this both versions run the same:

function hello(state)
    print("World")
end

function ugh(state)
    _ = hello(state) -- and w/ hello(state) only
end

ugh(state)

I just would like to know if there can be a time where this _ = some_function() is necessary?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Pwrcdr87
  • 935
  • 3
  • 16
  • 36
  • 2
    Have you see `_ = some_function()` or have you seen `_, val = some_function()`? Because the latter actually means something and is necessary. – Etan Reisner Jun 02 '15 at 19:26
  • @EtanReisner You're completely right, and I understand times when I would use the latter. No, that was not the case but I'll update w/ your questions. Thanks! – Pwrcdr87 Jun 02 '15 at 19:32
  • I can think of no cases in which `_ = some_function()` is at all meaningful (this isn't perl) but I can't say with complete certainty that is cannot be meaningful (especially as I haven't paid attention to lua 5.2 or 5.3 much at all). – Etan Reisner Jun 02 '15 at 19:35
  • @EtanReisner Thanks Etan. From my attempts so far I don't see any difference. But I am by no means as knowledgeable or experienced as a lot of members here. Thanks – Pwrcdr87 Jun 02 '15 at 20:07
  • It's not meaningful, and I've never seen it, ever. Can you give of someone actually doing this? – Mud Jun 02 '15 at 20:17
  • @Mud Someone here at work had used it for some R&D plug-in code. We are both new to programming, learning on the fly, and personally I've always just called the function I wanted to use. I don't believe he knew you could just call it. So he entered it that way to trigger its action. I informed him and removed the underscore all together. – Pwrcdr87 Jun 02 '15 at 20:23
  • @Mud Haha, thanks. Yeah, but I'm also a noob so I wanted to make sure I didn't miss something in my readings. Thanks again and I will update my original post. – Pwrcdr87 Jun 02 '15 at 20:38
  • 1
    `setmetatable(_G,{__newindex = function(_,n, v) if n == '_' then print('...') end end})` :) I think only side effect is overwrite global/upvalue. I see code that uses underscore as legal value. – moteus Jun 03 '15 at 07:57
  • @moteus thanks but is `_ = some_function()` considered the same usage as with your example with metatables? In my examples the person used it as a means to trigger another function to run. They were not using the underscore as a variable or variable location. – Pwrcdr87 Jun 03 '15 at 12:38
  • Sorry. code is is most a joke. But using underscore as variable is possible. http://mirven.github.io/underscore.lua https://github.com/Yonaba/Allen. – moteus Jun 03 '15 at 12:53
  • `_` is generally used in this context to let other developers know that a function is being called that returns a value, but the return value is irrelevant and will be discarded. the use of `_` is arbitrary and could be any other symbol/identifier. that said there could be negative performance implications if the return value is large and you hold onto the `_` variable for a long time. – Mike Corcoran Jun 03 '15 at 16:06

2 Answers2

3

In the example you wrote, _ is meaningless. In general, _ is used if a function is returning multiple values, and you don't need all of the returned stuff. A throw-away variable _ is, so to speak.

For example:

local lyr, needThis = {}
lyr.test = function()
    local a, b, c;
    --do stuff
    return a, b, c
end

Lets say, for such a function that returns multiple values, I only need the 3rd value to do something else. The relevant part would be:

_, _, needThis = lyr.test()

The value of needThis will be the value of c returned in the function lyr.test().

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
lyravega
  • 101
  • 5
  • thanks for your examples. I understand why you would use it as in your example. If you are receiving numerous variables in from a function and you only want to use a specific number of variables within a given function that it is called in. But what is happening here is that there are no variables returned for the function it is within. It just triggers that function to run, such as my `print` in my post. I hope that makes sense? haha – Pwrcdr87 Jun 03 '15 at 16:59
2

There is no benefit to _ = do_some_stuff(), rather using do_some_stuff() is fine and more accepted. The underscore provides no benefit when used in this way.

Thanks Etan Reisner and Mud for the help and clarification.

Pwrcdr87
  • 935
  • 3
  • 16
  • 36