www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Generic Class Alias Syntax

reply eris <jvburnes gmail.com> writes:
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
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
eris wrote:
 ...
 
 Is there any way to get around including the exclamation point?
 
 Thanks,
 
 eris
No. 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
parent reply eris <jvburnes gmail.com> writes:
Daniel Keep Wrote:

 
 
 eris wrote:
 ...
 
 Is there any way to get around including the exclamation point?
 
 Thanks,
 
 eris
No.
Succint! :-)
 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
parent bearophile <bearophileHUGS lycos.com> writes:
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
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent reply eris <jvburnes gmail.com> writes:
bearophile Wrote:

 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.
(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. :-) bye
Jun 04 2009
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
eris wrote:
 bearophile Wrote:
 
 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.
(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. :-) bye
And then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.
Jun 04 2009
next sibling parent BCS <none anon.com> writes:
Hello Robert,

 eris wrote:
 
 bearophile Wrote:
 
 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.
(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. :-) bye
And then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.
with a rope made of nested perens as it's the only thing you have handy.
Jun 05 2009
prev sibling next sibling parent Steve Teale <steve.teale britseyeview.com> writes:
Robert Fraser Wrote:

 
 That's a bad, cold, lonely day.  :-)
 
 bye
And then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.
((((((((((Why?))))))))))
Jun 05 2009
prev sibling parent eris <jvburnes gmail.com> writes:
Robert Fraser Wrote:

 eris wrote:
 bearophile Wrote:
 
 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.
(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. :-) bye
And then you come across Lisp, and see a glimmer of hope. An hour after that, you decide to hang yourself.
and then you see MetaLua and you say, "oooooh... pretty". http://metalua.luaforge.net/
Jun 08 2009