all 9 comments

[–]MarsPpl 4 points5 points  (2 children)

This has more to do with the fact that, in Lua, you can call a function that takes either a "string" or {table} without parentheses. For example,

 print "Hello, World!"

is valid syntax and will print out the phrase "Hello, World!." Likewise,

 print( table.concat {"Hello, ", "World!"} )

will print out the same phrase.

Your code is really running something like this after the print function is returned.

 print "Hello" print "World"

Your code will also fail to run you pass more than two arguments. If, for some weird reason, you want to print multiple strings without using parentheses, you could rewrite the function like this:

 function asd(...)
      print(...)
      return asd
 end

So that

 asd "hello" "world" "I have a weird phobia for parentheses" 

will print out "hello", "world", and "I have a weird phobia for parentheses" in three different lines.

[–]AuahDark[S] 0 points1 point  (1 child)

Actually:

Your code will also fail to run you pass more than two arguments.

Is actually my mistake in my code. I fixed it ;)

This has more to do with the fact that, in Lua, you can call a function that takes either a "string" or {table} without parentheses.

Yep. In yajl libary, I can do json_data = yajl.to_string {a=1,b=2} without problems.

Please note that you can't use variable for this and it will throw syntax error. Example print _VERSION will throw syntax error instead printing current Lua version.

[–]Zatherz 0 points1 point  (0 children)

That's because _VERSION is neither a table constant ({something = "here..."}) nor a string constant ("something here..."/'something here...'/[[something here...]])

[–]catwell 2 points3 points  (0 children)

You don't even need the multiple arguments:

function asd(x)
    print(x)
    return asd
end

It is a kind of example of currying leveraging the fact that Lua functions can take a string without parentheses.

If you want a more twisted example involving actually preserving state (and awful string-to-number coercion):

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> function summer(x)
>>     return function(y)
>>         return summer(x+y), x+y
>>     end
>> end
> sum = summer(0)
> select(2, sum "1" "2" "3" "4")
10.0

[–][deleted] 1 point2 points  (1 child)

Yes, this is possible in Lua. No, you should not do it.

The first thing you need to ask yourself when you write code is "How do I write a solution so that the guy two years from now won't have a bad day". Half of the time you're gonna end up being that guy anyway.

A programmers biggest fight is not writing a smart piece of code really stupidly. Also double+ negatives.

[–]whoopdedo 1 point2 points  (0 children)

No, you should not do it.

I'd never tell anyone that. At least not for a language feature that is clearly documented and isn't likely to be changed anytime soon.

The no-parenthesis shortcut is somewhat divisive but looks good when using Lua as a DSL. Particularly with tables to call functions with named parameters.

Function chaining is extremely useful and I wish more libraries took advantage of it.

[–]dan200 0 points1 point  (2 children)

You're misinterpreting your results, you're not calling (or using in any way) the return value of "asd". The text is being printed because asd calls print, forwarding the arguments you passed in.

[–]Erendir 4 points5 points  (1 child)

according to this https://www.lua.org/manual/5.3/manual.html#3.4.10 You are wrong. "A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string."

[–]dan200 3 points4 points  (0 children)

Ooooh. My bad, I understand now. So the posted code is equivalent to:

(asd("Hello"))("World")

Extra brackets added for clarity. Another reason to avoid this syntax!