What if we had functions
compile(String sourceCode)
and
runCompiled(BinaryData compiledCode)
? What could you do with it? Could it act as a closure, and bring really dynamic coding to traditionally static languages, perhaps allowing dynamically named variables and functions to languages without them (and much more!)? Could we store our code in a database and mix and match chunks, building programs based on a SQL query?
What do you think? Useful or useless?
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
I'm trying to think through how useful this would be (truth is I have NO idea, so I'm just thinking out loud - if I'm incorrect anywhere, input much appreciated!)
Firstly, in your proposed implementation, what would be the "compilable unit". I can see how you could compile classes on the fly, but unless a language supported mixins/partial classes, I'm guessing you couldn't compile methods only because there would be no place for them to go.
I'm also wondering about the syntax required to build up the compiled class. I think you'd also need a construct analogous to savecontent in ColdFusion so you would have the ability to use class templates. That could be very interesting as that'd be a macro system which would have a lot of interesting possibilities. But without that, concatenating strings to generate code (whether you're going to compile on the fly or save to disk) isn't any fun at all as your concatenator code obsfucates the class you're trying to generate making it hard to use.
Laurie Tratt also wrote an interesting posting:
http://tratt.net/laurie/tech_articles/articles/whe...He was suggesting that in fact a functioning macro system doesn't raise the abstraction layer of any language a set amount but rather helps sparse languages to become less so. I don't know that I agree, but it would suggest that macros in a language may be less important. I certainly think that there are issues in terms of maintainability with overuse of macros - especially for team development, but I'm not saying that'd make them worthless.
I'm guessing this wouldn't play the part of a ColdFusion style eval() which is more for smaller snippets of code, but there are definitely use cases for that in languages that don't allow for certain types of variability in any other way.
I'm not getting exactly how this would help to solve the static typing problem (unless you're talking about a macro that gens n-classes each with their own unique type signatures they'd accept which would make sense). Any thoughts on that?
I'd also love to hear what you think use cases might be and the languages that'd benefit most from this.
Great question! Any more thoughts?
Posted by
Peter Bell
on May 21, 2007 at 02:58 AM UTC - 6 hrs
@Sean - awesome link to the docs. To be honest, I hadn't even looked at what's new in Java 6. That does look like what I was talking about.
@Peter- as we chatted about this morning, I was sort of thinking out loud too, and hadn't really thought through it all. As far as the unit of compilable code - I guess I was thinking any legal code, and just insert it where ever, although I have no idea how that might work in Java. In CF, it would be tantamount to a <cfinclude> as I see it.
I agree with you about the need for a <cfsavecontent>-like construct. For the same reason Servlets suck when writing HTML and writing long queries sucks in concatenated strings, so too would trying to write a class (or any code more than a couple of lines) in typical strings.
Good going with pointing out macros - I hadn't even thought that far along, but they are quite relevant to the discussion. And, you are right that I am talking about more than a call to eval().
As for static typing - I didn't mean to imply typing at all - it was a poor choice of words. I was just thinking code that writes code (dynamic) as opposed to "static" code that just does what it does. I guess that often implies typing, but I wasn't trying to get at that here.
About use cases- who knows? It seems endless really. Anything from code that writes code (the low end) to (as far as my imagination takes me at the moment) storing code snippets in databases and constructing programs via SQL. I'm not sure why you might want to do that, but the idea of it seems powerful. Languages benefiting the most- anything without a good eval, really. But even the eval interpreter could be rendered useless if we took compilation to the level I'm thinking about here.
Most of this is doable anyway, but hack-like and I'm just wondering what would happen if we took it to the next level.
Posted by
Sam
on May 21, 2007 at 11:45 AM UTC - 6 hrs
Leave a comment