<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>エスブレイン &#187; call_user_func</title>
	<atom:link href="http://www.esbrain.com/tag/call_user_func/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.esbrain.com</link>
	<description>高い技術力・創造力・機動力でお客様のビジネスをさらに加速させます</description>
	<lastBuildDate>Fri, 09 Sep 2011 09:55:56 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.esbrain.com/tag/call_user_func/feed/" />
		<item>
		<title>Luaでcall_user_func 〜 クラスメソッドを変数で呼び出したい</title>
		<link>http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 09:51:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[call_user_func]]></category>
		<category><![CDATA[lua]]></category>

		<guid isPermaLink="false">http://www.esbrain.com/?p=526</guid>
		<description><![CDATA[PHPのcall_user_func関数って便利です。
イベント駆動系のプログラムを書くときには必須です。
そんな関数がLuaにはない・・・　（見つけられないだけかも知れませんが）
ということで、試作してみました。

f [...]]]></description>
			<content:encoded><![CDATA[<p>PHPのcall_user_func関数って便利です。</p>
<p>イベント駆動系のプログラムを書くときには必須です。</p>
<p>そんな関数がLuaにはない・・・　（見つけられないだけかも知れませんが）</p>
<p>ということで、試作してみました。</p>
<pre class="brush: cpp; title: ;">
function call_user_func(func, ...)
        local t = type(func)
        if t == 'function' then
                return func(...)
        elseif t == 'string' then
                if _G[func] == nil or type(_G[func]) ~= 'function' then
                        error(&quot;function is not defined '&quot;..func..&quot;'&quot;)
                end
                return _G[func](...)
        elseif t == 'table' then
                local _instance = func[1]
                local _method = func[2]
                if _instance == nil or _method == nil then
                        error(&quot;instance or method name is nil&quot;)
                end
                if _instance[_method] == nil then
                        error(&quot;class method is not defined '&quot;.._method..&quot;'&quot;)
                end
                return _instance[_method](_instance, ...)
        else
                error(&quot;func is not matched type '&quot;..type(func)..&quot;'&quot;)
        end
end

function funcA(name)
        print(&quot;My name is &quot;..name)
end

Class = {}

function Class:new(name)
        local t = {name = name}
        setmetatable(t, {__index = Class})
        return t
end

function Class:funcA()
        print(&quot;I am &quot;..self.name)
end

function Class:funcB(...)
        self:funcA()
        print(&quot;data are &quot;..table.concat({...}, &quot;,&quot;))
end

a = Class:new(&quot;taro&quot;)
a:funcA()
a:funcB(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;)

call_user_func({a, &quot;funcB&quot;}, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;)
call_user_func(funcA, &quot;jiro&quot;)
call_user_func(&quot;funcA&quot;, &quot;saburo&quot;)
</pre>
<p>実行結果は正常</p>
<pre>
I am taro
I am taro
data are 1,2,3
I am taro
data are a,b,c
My name is jiro
My name is saburo
</pre>
<p>19行目の_instance[_method](_instance, &#8230;)が肝です。</p>
<p>メソッドの第1引数はインスタンスを格納しなければならないオキテがあります。</p>
<p>よって、直接引数を&#8230;にしてしまうと、おかしなことになりますから注意！</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://www.esbrain.com/2009/06/lua%e3%81%a7call_user_func-%e3%80%9c-%e3%82%af%e3%83%a9%e3%82%b9%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e5%a4%89%e6%95%b0%e3%81%a7%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84/" />
	</item>
	</channel>
</rss>

