digitalmars.D.learn - What do you use opDispatch for?
- Philippe Sigaud <philippe.sigaud gmail.com> Mar 21 2010
- BCS <none anon.com> Mar 21 2010
- Philippe Sigaud <philippe.sigaud gmail.com> Mar 22 2010
- BCS <none anon.com> Mar 22 2010
- Mike Parker <aldacron gmail.com> Mar 22 2010
OK, I know opDispatch just appeared in DMD, but I remember a *huge* thread on
it, where people were jumping up and down waiting for it.
Me, I have no wonderful idea, though I feel some potential in it.
The only interesting use I found for now is making a class/struct extensible:
mixin template Extensible()
{
auto opDispatch(string s, T...)(T ts)
{
mixin("return " ~ s ~ "(this, ts);");
}
}
So given:
class C {
mixin Extensible;
}
and
class D : C {} // (D inherits from C's extensibility).
If can 'add' methods to C and D by defining external functions:
int foo(C c, int i) { return i;}
C c = new C;
c.foo(3); // becomes foo(c, 3).
It allows me to imitate what the compiler does for arrays:
"abc".toupper.reverse; // -> "CBA"
This functions can be templated, giving a new functionality to all extensible
classes:
string typeof(C)(C c) { return C.stringof;}
C build(C, T...)(C c, T t) if (is(C == class)) { return new C(t);}
C build(C, T...)(C c, T t) if (is(C == struct)) { return C(t);}
bool isSubtypeOf(C, D)(C c, D d) if (is(C : D)) { return true;}
bool isSubtypeOf(C, D)(C c, D d) if (!is(C : D)) { return false;}
etc.
So, is this a good idea or not? I use something like .build to map factory
functions on struct ranges, building another range of structs with different
values, but I do not _need_ mixin Extensible for this...
As for you, what are your experiences / projects with opDispatch?
Philippe
Mar 21 2010
Hello Philippe,OK, I know opDispatch just appeared in DMD, but I remember a *huge* thread on it, where people were jumping up and down waiting for it. Me, I have no wonderful idea, though I feel some potential in it. The only interesting use I found for now is making a class/struct extensible:
As for you, what are your experiences / projects with opDispatch?
My unitted type uses it for it's value<->unit properties to get a single point of definition for each unit: http://www.dsource.org/projects/scrapple/browser/trunk/units/si2.d I've also been thinking of a way to build a compile time LINQ like program. If the comparison and boolean operators are overloadable, you could build prepared SQL queries from expressions at compile time. using(myDatabase.tables.baz.bar) foreach(Row!(int) row; select!("baz.foo").whare(for.a != someInt && baz.c == bar.c)) do(row.foo);Philippe
... <IXOYE><
Mar 21 2010
--00032555990aa1abeb04826a5ba1 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Mar 21, 2010 at 23:02, BCS <none anon.com> wrote:My unitted type uses it for it's value<->unit properties to get a single point of definition for each unit: http://www.dsource.org/projects/scrapple/browser/trunk/units/si2.d
and generate what you need. Good idea. Why did you make OfType opDispatch a static function?I've also been thinking of a way to build a compile time LINQ like program. If the comparison and boolean operators are overloadable, you could build prepared SQL queries from expressions at compile time. using(myDatabase.tables.baz.bar) foreach(Row!(int) row; select!("baz.foo").whare(for.a != someInt && baz.c == bar.c)) do(row.foo);
If you have a limited number of methods like .whare, why use opDispach? (Sorry if my question is naive). Philippe --00032555990aa1abeb04826a5ba1 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sun, Mar 21, 2010 at 23:02, BCS <span dir=3D"= ltr"><<a href=3D"mailto:none anon.com">none anon.com</a>></span> wrot= e:<br><div>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt = 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex= ;"> My unitted type uses it for it's value<->unit properties to get a= single point of definition for each unit:<br> <a href=3D"http://www.dsource.org/projects/scrapple/browser/trunk/units/si2= .d" target=3D"_blank">http://www.dsource.org/projects/scrapple/browser/trun= k/units/si2.d</a><br> <br></blockquote><div><br>Ah, I see, that's nice. You use the received = string to feed another template and generate what you need. Good idea.<br>W= hy did you make OfType opDispatch a static function?<br><br>=A0</div><block= quote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left= : 1px solid rgb(204, 204, 204); padding-left: 1ex;"> I've also been thinking of a way to build a compile time LINQ like prog= ram. If the comparison and boolean operators are overloadable, you could bu= ild prepared SQL queries from expressions at compile time.<br> <br> using(myDatabase.tables.baz.bar) =A0 foreach(Row!(int) row; select!("b= az.foo").whare(for.a !=3D someInt && baz.c =3D=3D bar.c))<br> =A0 =A0 do(row.foo);<br></blockquote><div><br>If you have a limited number= of methods like .whare, why use opDispach? (Sorry if my question is naive)= .<br><br><br>=A0 Philippe <br></div></div><br> --00032555990aa1abeb04826a5ba1--
Mar 22 2010
Hello Philippe,On Sun, Mar 21, 2010 at 23:02, BCS <none anon.com> wrote:My unitted type uses it for it's value<->unit properties to get a single point of definition for each unit: http://www.dsource.org/projects/scrapple/browser/trunk/units/si2.d
template and generate what you need. Good idea. Why did you make OfType opDispatch a static function?
So that "OfType.someunit(val)" becomes a free function.I've also been thinking of a way to build a compile time LINQ like program. If the comparison and boolean operators are overloadable, you could build prepared SQL queries from expressions at compile time. using(myDatabase.tables.baz.bar) foreach(Row!(int) row; select!("baz.foo").whare(for.a != someInt && baz.c == bar.c)) do(row.foo);
opDispach? (Sorry if my question is naive).
The opDispatch gets used for the .a and .c bits. The above expression would get translated so that the !=/== expressions just collect values at runtime and pass them off as parameters of a prepared SQL statement whose string is built at compile time. For instance the above might result in the following string literal being used: "select baz.foo from baz join bar where a = % and baz.c == bar.c" -- ... <IXOYE><
Mar 22 2010
Philippe Sigaud wrote:As for you, what are your experiences / projects with opDispatch? Philippe
I was toying around with the idea of using it for a quick & dirty logger and came up with this: ************************** import std.stdio; import std.string; struct Log { File file; this(string filename) { file.open(filename, "w"); } void opDispatch(string s, string f = __FILE__, uint line = __LINE__, S...)(S args) { file.writeln("[", f, " ", line, "][", toupper(s), "]", args); } } ************************** And then use it like this: log.error("This is an error."); log.info("Wouldn't you like to know something?"); debug log.trace("Hey, this is happening now."); version(AudioStats) log.audio("Some stats: ", foo, bar); To me, this is much cleaner than the alternative, which would be something like: log.write("ERROR", "This is an error");
Mar 22 2010









BCS <none anon.com> 