Support cleaner usage of bind, by adding a built in _ variable
This commit is contained in:
parent
b3fa6405c5
commit
714e1c0cec
@ -497,6 +497,8 @@ namespace dispatchkit
|
|||||||
s.register_function(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s))), "dump_system");
|
s.register_function(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s))), "dump_system");
|
||||||
s.register_function(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1)), "dump_object");
|
s.register_function(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1)), "dump_object");
|
||||||
|
|
||||||
|
s.add_object("_", Placeholder_Object());
|
||||||
|
|
||||||
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
||||||
"bind");
|
"bind");
|
||||||
|
|
||||||
|
@ -206,6 +206,10 @@ namespace dispatchkit
|
|||||||
boost::shared_ptr<Proxy_Function> m_guard;
|
boost::shared_ptr<Proxy_Function> m_guard;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Placeholder_Object
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
class Bound_Function : public Proxy_Function
|
class Bound_Function : public Proxy_Function
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -244,7 +248,8 @@ namespace dispatchkit
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
while (barg != m_args.end() && !barg->is_unknown())
|
while (barg != m_args.end()
|
||||||
|
&& !(barg->get_type_info() == Get_Type_Info<Placeholder_Object>::get()))
|
||||||
{
|
{
|
||||||
args.push_back(*barg);
|
args.push_back(*barg);
|
||||||
++barg;
|
++barg;
|
||||||
@ -256,7 +261,8 @@ namespace dispatchkit
|
|||||||
++parg;
|
++parg;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (barg != m_args.end() && barg->is_unknown())
|
if (barg != m_args.end()
|
||||||
|
&& barg->get_type_info() == Get_Type_Info<Placeholder_Object>::get())
|
||||||
{
|
{
|
||||||
++barg;
|
++barg;
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,10 @@
|
|||||||
|
|
||||||
//This is a cheater function. The entire point of it is to return a variable
|
|
||||||
// with an unspecified type.
|
|
||||||
// The bind function will substitute any unknown object with those passed in
|
|
||||||
def _()
|
|
||||||
{
|
|
||||||
var i;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
def add(x, y)
|
def add(x, y)
|
||||||
{
|
{
|
||||||
return x + y;
|
return x + y;
|
||||||
}
|
}
|
||||||
|
|
||||||
var b = bind(add, 2, _());
|
var b = bind(add, 2, _);
|
||||||
var c = bind(b, 3);
|
var c = bind(b, 3);
|
||||||
|
|
||||||
print(b(4));
|
print(b(4));
|
||||||
@ -25,7 +16,7 @@ def concat(a,b,c,d)
|
|||||||
return to_string(a) + to_string(b) + to_string(c) + to_string(d);
|
return to_string(a) + to_string(b) + to_string(c) + to_string(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
var d = bind(concat, _(), " Hello ", _(), " World ");
|
var d = bind(concat, _, " Hello ", _, " World ");
|
||||||
print(d(1, 3));
|
print(d(1, 3));
|
||||||
//This checks to make sure arity is handled correctly:
|
//This checks to make sure arity is handled correctly:
|
||||||
//print(d(1, 3, 4));
|
//print(d(1, 3, 4));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user