lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




2017-07-04 5:53 GMT+02:00 Dirk Laurie <dirk.laurie@gmail.com>:
2017-07-04 1:31 GMT+02:00 Charles Heywood <vandor2012@gmail.com>:

> I personally feel that while it is nice, it'll lead to ambiguousness.

If that is a problem, how about:

debug.setmetatable(0,{__index=math})

print(math.exp(1):cosh())

or even

print((1):exp():cosh())

> On Mon, Jul 3, 2017, 18:29 Etiene Dalcol <dalcol@etiene.net> wrote:
>>
>> It looks very functional. I like it.
>> If it's not costy, it would be an incredible addition!
>>
>> 2017-07-03 23:44 GMT+01:00 Luiz Henrique de Figueiredo
>> <lhf@tecgraf.puc-rio.br>:
>>>
>>> > math.exp( 1 )->cosh()->round( 7 )
>>>
>>> You can almost do that right now:
>>>
>>> debug.setmetatable(0,{
>>>         __bor = function (x,y) return y(x) end
>>> })
>>>
>>> print(math.exp(1) | math.cosh)


... or you add the metatable to the function type:

Just throwing this in for the curious:

---
debug.setmetatable(function()end, {__mul = function(f,arg)
      if type(arg) == "table" then
        return function()
          return f(unpack(arg))
        end
      end
      return function(...)
        return f(arg,...)
      end
    end
  }
)
local s=math.sin * 0
local p = print * {"hello", s}

print(s())
p()
---

Output:
C:\tests>luajit main.lua
0
hello   function: 0x00178238

----

so in principle, it's not a syntax extension you would need :)

cheers,
Eike