A brief overview of type system metadata in various languages

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

Posted in Uncategorized | 24 Comments

Another Question: Information Sources

Where are you getting your new information on software development? No matter the language or platform. Who outside of the software realm is influencing your development learning?

Please let me know in the comments what books you are reading, what magazines, what websites, who are you following on twitter/blogs.

Interested in hearing from you!

-d

Posted in Uncategorized | 28 Comments

Sunday Thought: Embracing the Type System

I had a lot of fun talking with all of you on the benefits of dynamic languages on the previous post. Definitely getting a better picture of what working with a language like that is like listening to your stories, so thank you for sharing. I also was given a killer link to an over view of type systems that I want to share with you incase you missed it in the comments or when I tweeted about it.

What to know before debating type systems

Yup, I liked it that much. Easy to understand and gave me a fresh perspective.

Also during my read of the above link, was lead to this guy which blew my mind:

A type based solution to the strings problem

Damn, I need to spend more time in haskell. Interestingly, the little languages that he talks about in the post, and how we usually just use strings to represent them. That problem is one of the reasons why I love the little library called HtmlTags (although built by the Fubu guys has no dependencies on FubuMVC) - makes working with fragments of HTML so much easier. I need one of these for everything that I currently use strings for. :)

Which goes back to another thought of mine which is that we as developers depend far too heavily on primitives in our languages. We use strings to represent everything. Instead of taking the time to write a few app level primitives to wrap up these strings inside type’d goodness, we are content to just let them get passed around as anonymous objects. In .Net at the very least we could be using the BCL’s ‘SecureString’ when we need to lock strings down. You can also read an older post of mine on Business Primitives. And if you work with code that loves types as much as your compiler does you can get some really bad ass code to work with (my favorite is the way I can extend things in FubuMVC).

Do you have to write more code? Yes. Do you get value from doing so? Yes. Do you get enough value to make it worth it? That’s up to you, but I know that I do.

I would say that over the past year I have been looking at python and ruby and I just don’t get the same buzz out of those languages that I do out of languages like C# and Haskell. For my larger projects that is. I still maintain a few small sites and scripts in python (and I LOVE IT). but as my projects get bigger my brain is just wired into the type systems provided to me in a more static language. I love it, I can bend C#’s type system to my will in ways that crush my brain, much like the Haskell type system is currently crushing my brain.

Anyways, just my thoughts on this chill Sunday.

Posted in Uncategorized | 3 Comments

A Question to You: the Reader

A curiosity hit me today.

What is the value of dynamically typed languages in larger projects?

Do they have unique benefits?

Do they have unique negatives?

Load up the comments.

:)

-d

Posted in Uncategorized | 42 Comments

Sunday Thinking: Are you dealing with Risk or Uncertainty?

I was going through some notes of mine trying to clean up some older documents and I ran across this great quote from Frank Knight (an Economist who studied the effects of risk on market transactions) on the difference between risk and uncertainty:

“Uncertainty must be taken in a sense radically distinct from the familiar notion of Risk, from which it has never been properly separated…. The essential fact is that ‘risk’ means in some cases a quantity susceptible of measurement, while at other times it is something distinctly not of this character; and there are far-reaching and crucial differences in the bearings of the phenomena depending on which of the two is really present and operating…. It will appear that a measurable uncertainty, or ‘risk’ proper, as we shall use the term, is so far different from an unmeasurable one that it is not in effect an uncertainty at all.” http://en.wikipedia.org/wiki/Knightian_uncertainty

To review:

Risk is measurable: i.e. there is a 20% risk that this is going to fail

Uncertainty is not: I have no idea where this cave goes

I enjoy that he makes me think of the two terms as the totally distinct items they are. He then goes on to nail the point home by stating that the fact we can even measure risk makes it so radically different from uncertainty that we should question why we think of them in the same light at all.

I wanted to share this with you to encourage you to engage with your team and yourself to ask which of the two are you dealing with? With this enhanced vocabulary maybe we can all make better decisions.

Do you have any terms that people often think of as the same, but are not? (i.e. Complex and Complicated is another favorite of mine that people often confuse).

Happy Sunday,

-d

Posted in Small Ideas | 12 Comments