www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ensuring template argument is descendant of class

reply "Ross Hays" <test gmail.com> writes:
I am trying to see what I can make in the way of a game engine in 
D, and am currently trying to write a basic asset manager.

I have a base Asset class that all asset types are going to 
inherit; one such example of this is Texture. Now I am using the 
following code on the loadAsset method in the file manager...

     T loadAsset(T)(in string filename)
         if (cast(Asset)T)
     {...

Making a call such as...

     auto tex = loadAsset!Texture("/test.png")

...results in the following error at compile time...

     source\engine\assets.d(28): Error: Cannot interpret Texture 
at compile time

I am not sure what the best way to do something like this would 
be. I could always just take the template argument, and return an 
Asset object, but then the calling code would need to cast the 
object returned and I am trying to avoid that if possible.

Thanks!

Ross
Dec 12 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 12 December 2013 at 21:51:14 UTC, Ross Hays wrote:
         if (cast(Asset)T)
Try this instead: if(is(T : Asset)) the is thing checks types. You can do is(T == Asset) for the specific class, or is(T : Asset) which means it implicitly converts to it - in otherworts, if T is Asset or a child of Asset.
Dec 12 2013
parent reply "Ross Hays" <test gmail.com> writes:
On Thursday, 12 December 2013 at 21:53:20 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 12 December 2013 at 21:51:14 UTC, Ross Hays wrote:
        if (cast(Asset)T)
Try this instead: if(is(T : Asset)) the is thing checks types. You can do is(T == Asset) for the specific class, or is(T : Asset) which means it implicitly converts to it - in otherworts, if T is Asset or a child of Asset.
Okay thank you, that seems to clear up the error. I found http://dlang.org/expression.html#IsExpression and had no idea is could be used with that syntax. The more D I learn the more counter-intuitive I find some aspects of the language syntax. A shame, that was my main point to start using it. Oh well! Thanks
Dec 12 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 12 December 2013 at 22:05:17 UTC, Ross Hays wrote:
 http://dlang.org/expression.html#IsExpression and had no idea 
 is could be used with that syntax. The more D I learn the more 
 counter-intuitive I find some aspects of the language syntax.
Yeah, the is expression is one of the strangest things in D. Especially if you use it to deconstruct arrays and templates! But between the two simpler forms of is(A == B) and is(A : B), and http://dlang.org/phobos/std_traits.html you can get a lot done and it isn't so bad looking.
Dec 12 2013
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Dec 12, 2013 at 11:23:15PM +0100, Adam D. Ruppe wrote:
 On Thursday, 12 December 2013 at 22:05:17 UTC, Ross Hays wrote:
http://dlang.org/expression.html#IsExpression and had no idea is
could be used with that syntax. The more D I learn the more
counter-intuitive I find some aspects of the language syntax.
Yeah, the is expression is one of the strangest things in D. Especially if you use it to deconstruct arrays and templates! But between the two simpler forms of is(A == B) and is(A : B), and http://dlang.org/phobos/std_traits.html you can get a lot done and it isn't so bad looking.
Yeah, is-expression syntax is one of the dark, ugly corners of D that unfortunately we're stuck with, because changing it now will totally break a LOT of code for merely cosmetic reasons. I honestly wish that one day this mess could be cleared up, though I'm not holding my breath for it. T -- "Computer Science is no more about computers than astronomy is about telescopes." -- E.W. Dijkstra
Dec 12 2013
parent reply "Ross Hays" <test gmail.com> writes:
 Yeah, is-expression syntax is one of the dark, ugly corners of 
 D that
 unfortunately we're stuck with, because changing it now will 
 totally
 break a LOT of code for merely cosmetic reasons. I honestly 
 wish that
 one day this mess could be cleared up, though I'm not holding 
 my breath
 for it.


 T
I know this is probably obvious, but if D were allowed to just make one big breaking change for D3 or something, and fix every dark corner it has. Would that really be so bad? I know it would break things but... I kind of long for it.
Dec 12 2013
parent reply "Brian Rogoff" <brogoff gmail.com> writes:
On Friday, 13 December 2013 at 05:22:26 UTC, Ross Hays wrote:
 Yeah, is-expression syntax is one of the dark, ugly corners of 
 D that
 unfortunately we're stuck with, because changing it now will 
 totally
 break a LOT of code for merely cosmetic reasons. I honestly 
 wish that
 one day this mess could be cleared up, though I'm not holding 
 my breath
 for it.


 T
I know this is probably obvious, but if D were allowed to just make one big breaking change for D3 or something, and fix every dark corner it has. Would that really be so bad? I know it would break things but... I kind of long for it.
I'm sympathetic to this POV, or something similar. For example, a multiyear plan to deprecate the 'is' syntax and replace it with something better, in the current D. I'm not a fan of gratuitous changes which make the language unstable, but if there were a pleasing design to replace 'is' I'd like to think that D users could change the relevant sources given a long enough deprecation window. Changing every dark corner and every poor choice is too much. For instance, a lot of people think that immutability and 'nothrow' should have been the default, but changing that would be dramatic and probably will have to wait for a D3 or new language. -- Brian
Dec 12 2013
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Dec 13, 2013 at 06:42:26AM +0100, Brian Rogoff wrote:
 On Friday, 13 December 2013 at 05:22:26 UTC, Ross Hays wrote:
Yeah, is-expression syntax is one of the dark, ugly corners of D
that unfortunately we're stuck with, because changing it now will
totally break a LOT of code for merely cosmetic reasons. I honestly
wish that one day this mess could be cleared up, though I'm not
holding my breath for it.


T
I know this is probably obvious, but if D were allowed to just make one big breaking change for D3 or something, and fix every dark corner it has. Would that really be so bad? I know it would break things but... I kind of long for it.
I'm sympathetic to this POV, or something similar. For example, a multiyear plan to deprecate the 'is' syntax and replace it with something better, in the current D. I'm not a fan of gratuitous changes which make the language unstable, but if there were a pleasing design to replace 'is' I'd like to think that D users could change the relevant sources given a long enough deprecation window.
I vote for this. Good luck convincing Walter, though.
 Changing every dark corner and every poor choice is too much. For
 instance, a lot of people think that immutability and 'nothrow'
 should have been the default, but changing that would be dramatic
 and probably will have to wait for a D3 or new language.
[...] Not to mention such a big change (or such numerous fundamental changes) would probably introduce new dark corners that then need fixing. :) Language design ain't easy. T -- It is of the new things that men tire --- of fashions and proposals and improvements and change. It is the old things that startle and intoxicate. It is the old things that are young. -- G.K. Chesterton
Dec 12 2013
next sibling parent "Ross Hays" <throwaway email.net> writes:
I remember seeing some posts about D and how the way it sounded 
was that D kind of filled the role of C++, without all the burden 
of years of full backwards comparability. I wouldn't dare say it 
is that bad, but this is how the problem starts I feel. I would 
love to see a release cycle like you mentioned. Something like 
OpenGL deprecating would be great but I could see issues for 
things like keywords (IsExpression for example).

But what do I know. I've never written a programming language. I 
just hope the end result isn't wait for another!

Ross
Dec 12 2013
prev sibling parent "Brian Rogoff" <brogoff gmail.com> writes:
On Friday, 13 December 2013 at 05:55:09 UTC, H. S. Teoh wrote:
 On Fri, Dec 13, 2013 at 06:42:26AM +0100, Brian Rogoff wrote:
 I'm sympathetic to this POV, or something similar. For 
 example, a
 multiyear plan to deprecate the 'is' syntax and replace it with
 something better, in the current D. I'm not a fan of gratuitous
 changes which make the language unstable, but if there were a
 pleasing design to replace 'is' I'd like to think that D users 
 could
 change the relevant sources given a long enough deprecation 
 window.
I vote for this. Good luck convincing Walter, though.
Step 1 is to come up with a set of better design alternatives, and a transition plan, etc. I'm pretty sure I've read posts from Walter on this forum that agree with the current 'is' is far from ideal, so maybe convincing him is not as hard as you think, though I imagine he has bigger fish to fry these days. Still, it would be encouraging if the D community could address the general issue of *not* being forced to keep bad designs forever. -- Brian
Dec 13 2013