Coder Social home page Coder Social logo

How to use __call about middleclass HOT 2 CLOSED

kikito avatar kikito commented on August 22, 2024
How to use __call

from middleclass.

Comments (2)

qaisjp avatar qaisjp commented on August 22, 2024

Not quite sure what you're doing there, but off the top of my head (and writing this on mobile), __call works like this in pure Lua:

local tab=setmetatable({},
{
__call=function(t, ...)
  print("Args:", ...)
end
})

tab.regulartable = "yeah"

tab("Hello", "world")
-- > Args:  Hello  World

tab(1, 2, 3)
-- > Args:  1  2  3

print(tab.regulartable)
-- > yeah

from middleclass.

kikito avatar kikito commented on August 22, 2024

@CriztianiX I think you are mistaking the __call metamethod with the __index metamethod.

As @qaisjp says, __call basically allows "using an instance of a class as a function". So if the class S has the __call method defined, and s is an instance of S, you can do s('foo').

__index is the metamethod to call when "you call something which does not exist in a table". Unfortunately I need to use __index to implement the basic functionality of middleclass, so I can't support it.

In your particular case, you can get around this issue by using a bit of metaprogramming, defining all the methods you need. It should look similar to this:

local class = require "lua-resque.deps.middleclass"
local redis = require 'redis'

local Resque_Redis = class('Resque_Redis')
function Resque_Redis:initialize(sredisServer, sredisDatabase)
  local client = redis.connect('127.0.0.1', 6379)
  self.client = client
  return client
end

local redis_methods = { 'sadd', 'srem' } -- add more methods here
for i=1,#redis_methods do
  local method = redis_methods[i]
  Resque_Redis[method] = function(self, ...)
    return self.client[method](...)
  end
end

Add the methods you need to the table redis_methods and they will be available for each instance.

from middleclass.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.