This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
EveryOS's profile picture

mt's: calling strings?

Started by EveryOS, 25 November 2016 - 05:53 PM
EveryOS #1
Posted 25 November 2016 - 06:53 PM
Is it possible to use mt's to make a string callable?

For example:

local stringCall = function(...)
end

local stringMT= {
  __call --or something like that
    = stringCall
}
--... you know the rest

So that I can do this:

'string'(argz)

?
Admicos #2
Posted 25 November 2016 - 07:40 PM
Probably, but why?
EveryOS #3
Posted 25 November 2016 - 08:30 PM
I'm planning an making 2 api's, Libra and WEBer.
In the Libra api, I decided it's better to overwrite a nasty message saying 'attempt to call string' than overwrite somebodies beautiful functions.

So I choose to use strings.

Here's an example:
Spoiler

--Another reason I want a __call mt: to call a table.
libra '1.0.00'
--Three sample setups in the enviroment
--These can easily be handled with __index and __newIndex mt's
--Just I don't know of a __call mt
Env.libra['paths'] = {
  ['libra'] = ':/libra/libraries/' --Maybe default this?
  ['system'] = ':/libra/system/plugins/'--This too?
  ['app'] = ':/myApp/libra/plugins/'

--Here's where to pay attention.
'libra' libra.req {'optparse', 'WEBer'} --Of course, the WEBer library is a library (multiple scripts, including the libra plugin loader), while the WEBer api is just one script with global functions Aka an api
'app' libra.req 'SomeLibrary'
'system' libra.install 'Admicos::SomeLibrary::1.0'
'system' libra.req 'Admicos::SomeLibrary'
EveryOS #4
Posted 25 November 2016 - 08:47 PM
Edit:
Oh great.
There actually is a __call mt, but it only works on tables.
Is there any way I can call a string, though?
H4X0RZ #5
Posted 26 November 2016 - 01:50 AM
Edit:
Oh great.
There actually is a __call mt, but it only works on tables.
Is there any way I can call a string, though?

Technically it's possible to get the metatable for strings, but bios.lua blocks access to it so you would have to modify that