Send to Printer

development

So much work

July 24, 2004 1:10:05.813

This is interesting - a .NET (C#) developer looking at what it would take to add an attribute to an object at runtime:

I wanted to be able to add properties on the fly.  I've changed the idea a bit to look them up on the fly versus adding them.  Using psuedo-code:

// expandable object
MyObject o = new MyObject();

// add the property
o.AddProperty("FirstName", "Justin");

I'm not using these properties in code anywhere.  I'm actually feeding the object to a template system (the C# port of StringTemplate in this case).  Now StringTemplate supports reading properties in the templates like so...

$p.FirstName$

But unfortunately the MyObject type doesn't have a FirstName property.  And StringTemplate uses the Type to do reflection.  Anyway, during my research of IExpando (much like IDispatchEx), I noticed the interface IReflect.  Looking at the docs for it, it contains all the most common methods that people use on Type when performing reflection (GetProperty, GetMethod, etc.).  So if I change StringTemplate to use IReflect vs. the Type class, then I could implement IReflect myself.  Of course, there are problems with this; mainly the fact that PropertyInfo is an abstract class (MemberInfo, the parent class, is also abstract).  So I would have to write implementations of those.

Hm...this seems like more effort than its worth.  Maybe I should just do something like this...

$p.Attributes.FirstName

Attributes would be an IDictionary.  Then I can extend StringTemplate to look at the object.  If it is IDictionary, then use the property name as the key.  The former definitely has more of a "coolness" factor, but the latter would take like 15 minutes.

Heh. As opposed to the following in Smalltalk:

myObject class addInstVarName: 'firstName'. This is one of those telling differences between Smalltalk and languages like Java and C# - it's not that you can't accomplish the same things - it's that you don't bother because of all the work involved. Smalltalk makes the simple things simple, and the hard things possible. Languages like C# and Java make the simple things possible, and the hard things painful...

Comments

Some credit due...

[Ian Bicking] July 24, 2004 12:29:09.902

I think C# and Java deserve some credit, in that you actually can do some of this stuff. Well, maybe... the quoted .NET developer is just theorizing an implementation, it may not have worked. (Considering the number of steps he's considering, probably one of them won't work like he expects, maybe in a way that makes the idea infeasible)

Anyway, they deserve some credit, because in languages like C++ you can't do this, no matter how hard you try. At least Java and C# offer some advancements in terms of reflection, even if it's not very pleasant. It's only the first step in a good meta-programming system, which Java still doesn't have (and probably C# doesn't have, but I don't know), but at least they moved forward a little.

Um ...

[James Ladd] July 24, 2004 19:32:42.850

Ian I disagree. You can do those things in C++. The whole point here is that it is easy in smalltalk and provided in the basic libraries/language. The other languages allow it to be done but it takes a lot more effort and sadly the end result doesnt always look and feel native to the language, as it does in smalltalk.

Re: So much work

[ James Robertson] July 24, 2004 22:11:15.364

Comment on So much work by James Robertson

What James Ladd said. The point is that you can accomplish these tasks in other languages/platforms - but it's so much harder than Smalltalk that most people just won't bother

in C++, really?

[Ian Bicking] July 24, 2004 23:53:22.559

I don't have much C++ experience, but you can change classes at runtime in C++? And look up instance variables by name, or query what variables exist, etc?

[] July 25, 2004 17:38:12.433

Ian, in C++ you accomplish this by writing "scads" of code (as JR would put it) and the resulting code would not look like native C++. This is the point. You could achive something more native by fiddling the vtable but thats another topic. In C++ you would basically be writing an object layer.

So, not so much with C++

[Ian Bicking] July 25, 2004 20:10:58.255

That's what I figured. Obviously any Turing Complete language can be used to implement Smalltalk, but that's obviously not the point. If you have to build a whole object system to implement reflection, then the language doesn't have reflection (though your program may). Maybe you can mess around with vtables -- maybe that's even within the documented scope of C++ (though I suspect not) -- but it's clearly a hack. So I reassert: Java deserves some credit.

 Share Tweet This