So given that a class that is nothing more than a data bag. After all, each object is just a bag of dict’s which are pointers to some byte array some place. What can we introspect about the byte array in each language?
The following is my attempt to document my metadata experiments.
Let’s start with .Net, its what I know the best and what I am ultimately comparing everything too.
.Net
public class Loan { public decimal Amount { get; set; } } var aLoan = new Loan {Amount=5}; aLoan.GetType(); //an instance of Type filled with data about Loan for(var prop in aLoan.GetType().GetProperties()) { Console.WriteLine(prop.Name); } aLoan.GetType().GetProperty("Amount"); //returns an instance of PropertyInfo
Links:
Type: http://msdn.microsoft.com/en-us/library/system.type.aspx
PropertyInfo: http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx
Ruby
class Loan attr_accessor :amount def initialize(initialAmount) @amount = initialAmount end end aLoan = Loan.new 5 aLoan.class #Loan aLoan.method(:amount) #a method object
After playing around, I couldn’t find anyway to get the type returned by the ‘amount’ function. This is exactly what I expected, but wanted to confirm. I could get the number of arguments via the ‘arity’ method. That said, I was able to extract a lot more information than I had expected. Special thanks to @jflanagan for the much need schooling in ruby.
Links:
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Accessor_Methods
http://ruby-doc.org/core-1.9.3/Method.html
Python
class Loan(object): def __init__(self, initialAmount): self._amount = initialAmount @property def amount(self): return self._amount aLoan = Loan(5) aLoan.__class__ #Loan aLoan.amount #nope, this returns 5 dir(aLoan) #return 'all the things'
Links
http://docs.python.org/library/functions.html#property
JavaScript
function Loan(initialAmount) { //as recommended by Ryan Rauh this.amount = initialAmount; return this; } var aLoan = new Loan(5); typeof(aLoan); //object Object.getPrototypeOf(aLoan).constructor.name; //Loan for(prop in aLoan) {console.log(prop)}; //prop is a string typeof(aLoan['amount']); //number //this is actual a typeof(5)
I was again surprised at how much I could extract. Special thanks to @rauhryan for the JavaScript schooling.
Beyond my reach (for now)
So I wanted to do some other languages outside my comfort zone like lisp, haskell, and erlang. But my brain was exploded by the very different nature of the languages. What I do have is how I would set the structure up roughly in the various languages. I hope to come back overtime and update this as I learn or people share with me.
Lisp
('Loan, ('Amount, 20))
Haskell
data Loan = Loan { Amount :: Int }
http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/moretypes.html
Erlang
-record(Loan, { Amount })
Links:
http://www.erlang.org/doc/reference_manual/records.html
Any feedback on how to do these kinds of things in other languages will be most appreciated.
-d