I know this isn't what you asked, but
return throw(({"Unsupported type conversion for the object", backtrace()}));
is much simpler written as
| error("Unsupported type conversion for the object");
if I didn't misunderstand something here...
/ Mirar
Previous text:
2003-02-10 19:09: Subject: Implicit vs. explicit type casting with Pike
Hey all,
Consider the following code:
class settable { private mapping data = ([]);
mixed [](string what) { if (data[what]) return data[what]; return ([])[0]; }
mixed []=(string what, mixed contents) { data[what] = contents; return contents; }
mixed ->(string what) { return [](what); }
mixed ->=(string what, mixed contents) { return []=(what, contents); } array _indices() { return indices(data); }
array _values() { return values(data); }
mixed cast(string rtype) { switch(rtype) { case "mapping": write("cast called - they want a mapping\n"); return data; default: return throw(({"Unsupported type conversion for the object", backtrace()})); } } };
int main() { object s = settable();
s["test"] = 1; s->test1 = 2; s->test2 = ([]);
write("settable: %O\n%O\n", indices(s), values(s)); m_delete((mapping)s, "test1");
return 0; }
The above program works as expected, but if I remove the explicit cast, m_delete will throw an error:
Bad argument 1 to m_delete(). Expected object with _m_delete Unknown program: m_delete(ptest()->settable(),"test1") ptest.pike:59: ptest()->main()
Which is incorrect as per the m_delete synopsis:
mixed m_delete(object|mapping map, mixed index)
Two questions I have are:
- is there a way to define the default implicit cast for an object, so that
the above code would invoke the cast() method when m_delete is called without the explicit mapping cast, or
- is there any way to install a hook for an object to intercept index
deletion in the above code (without the cast)?
TIA,
marek
/ Brevbäraren