digitalmars.D - Generic Class Alias Syntax
- eris (30/30) Jun 04 2009 Previously we were having fun with generic programming. When last we tu...
- Daniel Keep (6/13) Jun 04 2009 No.
- eris (6/23) Jun 04 2009 Okay. I just assumed that since it was aliased .. and .. I included a t...
- bearophile (13/14) Jun 04 2009 If you want to instantiate a template and you have a value of the actual...
- bearophile (6/7) Jun 04 2009 !(int) tells the template what type is T. Somewhere you have to tell it ...
- eris (6/12) Jun 04 2009 (1) Cake
- Robert Fraser (3/19) Jun 04 2009 And then you come across Lisp, and see a glimmer of hope. An hour after
- BCS (2/31) Jun 05 2009 with a rope made of nested perens as it's the only thing you have handy.
- Steve Teale (2/9) Jun 05 2009 ((((((((((Why?))))))))))
- eris (3/23) Jun 08 2009 and then you see MetaLua and you say, "oooooh... pretty".
Previously we were having fun with generic programming. When last we tuned in I had... An enum: enum ORDER {DESCENDING, ASCENDING}; An Interface: public interface Ranker(T) { bool submit(T value); // submits a value of type T to be included in top 'n' values, true if added or already present bool expire(T value); // removes a previously included value of type T from top 'n' values, false if non-existant T extreme(); // returns the value of type T from Ranker which is the current top value } And a Class Template: class Rank(T, ORDER rankOrder = ORDER.ASCENDING) : Ranker!(T) From this and some implementation magic we can create objects that track the top (or bottom) 'n' elements submitted to it. So far, so good. I'm trying to create an alias that lets me create these objects like this: auto top32 = MaxRank(int)(32); Where '32' is the number of top elements to track. So we create template aliases like this: template MinRank(T) { alias Rank!(T, ORDER.DESCENDING) MinRank; } template MaxRank(T) { alias Rank!(T, ORDER.ASCENDING) MaxRank; } And that works, but only if I create the MaxRank by including an exclamation point thusly: auto top32 = MaxRank!(int)(32); Well, that kind of defeats one of the purposes of creating a template alias. With just a little more effort I can just type: auto top32 = Rank!(int, ORDER.ASCENDING); Is there any way to get around including the exclamation point? Thanks, eris
Jun 04 2009
eris wrote:... Is there any way to get around including the exclamation point? Thanks, erisNo. If you want more explanation than that: No, because if you did, the parser wouldn't be able to tell when something is supposed to be a type and when it's supposed to be an expression. It's just *one* extra character...
Jun 04 2009
Daniel Keep Wrote:eris wrote:Succint! :-)... Is there any way to get around including the exclamation point? Thanks, erisNo.If you want more explanation than that: No, because if you did, the parser wouldn't be able to tell when something is supposed to be a type and when it's supposed to be an expression.Okay. I just assumed that since it was aliased .. and .. I included a type that matched the template parameter it should be able to sort that out. Re-reading the syntax specs should enlighten me.It's just *one* extra character...I know. It's not so much the one character and obviously aliases achieve much more than just compressing out a single character. In fact, there's really no hiding that it's a template. You have to include the 'int' anyhow -- either explicitly in the alias def or in the expression itself. thx
Jun 04 2009
eris:Okay. I just assumed that since it was aliased .. and .. I included a type that matched the template parameter it should be able to sort that out. Re-reading the syntax specs should enlighten me.<If you want to instantiate a template and you have a value of the actual type, then there is often a way to solve the problem. You can write a small function that performs the trick. For example you have the templated class (or struct in D2) Foo!(T), then you can define a small foo() like: Foo!(T) foo(T)(T x) { return new Foo!(T)(x); } Now you can instantiate Foo with: auto fint = foo(10); auto fdouble = foo(10.0); No stinking bangs (!) to be seen :-) But I don't think this can be used in your situation. Bye, bearophile
Jun 04 2009
eris:Is there any way to get around including the exclamation point?<!(int) tells the template what type is T. Somewhere you have to tell it what type of items you want to put inside it. The alternative is like the old Java, where your collections contain references to Object, used to store wrappers (boxes) like Integer, etc, and you have to cast the items you pull out of them. So I don't understand what you want. Bye, bearophile
Jun 04 2009
bearophile Wrote:eris:(1) Cake (2) Eat it too Sometimes you realize that you don't get to redefine the language syntax. That's a bad, cold, lonely day. :-) byeIs there any way to get around including the exclamation point?<!(int) tells the template what type is T. Somewhere you have to tell it what type of items you want to put inside it. The alternative is like the old Java, where your collections contain references to Object, used to store wrappers (boxes) like Integer, etc, and you have to cast the items you pull out of them. So I don't understand what you want.
Jun 04 2009
eris wrote:bearophile Wrote:And then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.eris:(1) Cake (2) Eat it too Sometimes you realize that you don't get to redefine the language syntax. That's a bad, cold, lonely day. :-) byeIs there any way to get around including the exclamation point?<!(int) tells the template what type is T. Somewhere you have to tell it what type of items you want to put inside it. The alternative is like the old Java, where your collections contain references to Object, used to store wrappers (boxes) like Integer, etc, and you have to cast the items you pull out of them. So I don't understand what you want.
Jun 04 2009
Hello Robert,eris wrote:with a rope made of nested perens as it's the only thing you have handy.bearophile Wrote:And then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.eris:(1) Cake (2) Eat it too Sometimes you realize that you don't get to redefine the language syntax. That's a bad, cold, lonely day. :-) byeIs there any way to get around including the exclamation point?<!(int) tells the template what type is T. Somewhere you have to tell it what type of items you want to put inside it. The alternative is like the old Java, where your collections contain references to Object, used to store wrappers (boxes) like Integer, etc, and you have to cast the items you pull out of them. So I don't understand what you want.
Jun 05 2009
Robert Fraser Wrote:((((((((((Why?))))))))))That's a bad, cold, lonely day. :-) byeAnd then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.
Jun 05 2009
Robert Fraser Wrote:eris wrote:and then you see MetaLua and you say, "oooooh... pretty". http://metalua.luaforge.net/bearophile Wrote:And then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.eris:(1) Cake (2) Eat it too Sometimes you realize that you don't get to redefine the language syntax. That's a bad, cold, lonely day. :-) byeIs there any way to get around including the exclamation point?<!(int) tells the template what type is T. Somewhere you have to tell it what type of items you want to put inside it. The alternative is like the old Java, where your collections contain references to Object, used to store wrappers (boxes) like Integer, etc, and you have to cast the items you pull out of them. So I don't understand what you want.
Jun 08 2009