Statically Typed

because Hindley-Milner rocks

Pragmatic Programmer: Auto Code Generation 2


Following up on yesterday I’ve got a script I use for generating immutable data types.  These are generally what I form from a DB query.  The script is very, very simple although the very imperative nature of it is hard to miss:

def CreateImmutable( _className, _typePairs ):
   print "class " + _className.capitalize()
   print "{"
   print "public:"
   constructor = "\t" + _className + "("
   for varName, varType in _typePairs.iteritems():
      constructor += varType + " _" + varName + ", "
   print constructor + ") : "
   for varName, varType in _typePairs.iteritems():
      print "\t   m_" + varName.capitalize() + "(_" + varName + "),"
   for varName, varType in _typePairs.iteritems():
      print "\t" + varType + "\tGet" + varName.capitalize() + "() const { return m_" + varName.capitalize() + "; }"
   print "protected:"
   print "private:"
   for varName, varType in _typePairs.iteritems():
      print "\t" + varType + "\tm_" + varName.capitalize() + ";"
   print "};"

It’s also obvious that it’s not “finished.”  I still have to go in and delete the last commas from the constructor.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on December 31, 2009 by in Pragmatic Programmer.
%d bloggers like this: