Jump to content

  • Log In with Google      Sign In   
  • Create Account


The template paradox

  • You cannot reply to this topic
8 replies to this topic

#1 NEXUSKill   Members   -  Reputation: 456

Like
1Likes
Like

Posted 13 March 2014 - 09:31 AM

I just wrote this, it works perfectly, but I don't know what to make of it in terms of what the concept means...

 

public class SerializedData<DataType> where DataType: SerializedData<DataType>
 
This is a template class in which its template type must be derivate of the same clase with the same template type.... sounds terribly weird

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272



Sponsor:

#2 TheComet   Members   -  Reputation: 1489

Like
0Likes
Like

Posted 13 March 2014 - 09:45 AM

Why would you serialize your own serializing class?


YOUR_OPINION >/dev/null


#3 NEXUSKill   Members   -  Reputation: 456

Like
0Likes
Like

Posted 13 March 2014 - 09:50 AM

each class implements its own specialized serialization, they inherit many common methods from this class and any loaded resources are held in a static hash implemented in the base serialization class.


Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272



#4 unbird   Crossbones+   -  Reputation: 4973

Like
1Likes
Like

Posted 13 March 2014 - 10:27 AM

It has a name (and of course its usefulness) : curiously recurring template pattern.

#5 NEXUSKill   Members   -  Reputation: 456

Like
0Likes
Like

Posted 13 March 2014 - 10:37 AM

Oh god I laughed so hard when I read the pattern name! xD


Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272



#6 Ravyne   Crossbones+   -  Reputation: 7116

Like
1Likes
Like

Posted 13 March 2014 - 11:37 AM

I don't know if this is the CRTP actually -- it looks similar-ish, but CRTP is when a a derived class inherits from a base which takes the derived class as a template parameter; here, unless I misread (which could be, I'm not sure what the language in question here is... C#? Java?), you have a templates class that requires its template argument to be of the same type.

 

OP, which language is this?

 

It would seem on the surface to be infinitely recursive, unless at some point it a default argument kicks in and bottoms out the recursion -- That, or it doesn't actually recurse at all and only goes 2 levels deep. But its hard to say -- templates/generics in any language get weird.

 

Nevermind, I see how it goes now, but I remain unconvinced that this is CRTP.


Edited by Ravyne, 13 March 2014 - 11:41 AM.


#7 unbird   Crossbones+   -  Reputation: 4973

Like
2Likes
Like

Posted 13 March 2014 - 12:45 PM

Yeah, but it's the preparation for one, since C# needs this so-called generic type constraint (the part after 'where') to make it actually work. The compiler would complain otherwise. Nexus, can you confirm that you later use it like so:
 
public class SomeData : SerializedData<SomeData> ...


#8 Nypyren   Crossbones+   -  Reputation: 4167

Like
1Likes
Like

Posted 13 March 2014 - 12:58 PM

It's a CRGC (curiously recurring generic constraint), not CRTP.  I don't know if anyone's ever formally called it that, but that's what I call it.

 

Like unbird says, using the CRGC requires that the parameter use a CRTP.


Edited by Nypyren, 13 March 2014 - 01:00 PM.


#9 NEXUSKill   Members   -  Reputation: 456

Like
0Likes
Like

Posted 14 March 2014 - 07:42 AM

Yes, this is the case, this class is meant to be inherited by other classes that need to be serialized and they must set themselves as the template parameter. Though it is true that this class declaration by itself does not force its data to inherit from it, it could be the same class with the same type, but that would be pretty pointless in my project.

It goes just as unbird said.


Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272







PARTNERS