49 points · by trapper · 17y ago · 35 comments
case class Mountain( val name: String, val latitude: Int, val longtitude: Int, var country: String)
For the above code you get a nice factory like
Mountain('name', 11, 12, 'nowhere')
with hashCode, toString and equals. Plus that you can use this class to decompose it using pattern matching:
mountain match { case Mountain(name, _, _, country) => println(name + ' located in ' + country) }
The future is here - it's just not evenly distributed:
Will other tools find this code? E.g. I think javadoc is source-based, and I don't know if it will invoke such annotations. I would expect the annotation spec to require it, for annotations that can create methods etc, but I'm just not that familiar with the spec.
BTW: great presentation video: showing the problem, showing the solution, showing how to get started. It might seem like overkill, but that's the way to sell stuff (though this one seems to be under the MIT license, ie free).
(It's not just parsing. We have to write little tools to take the parsed code, then infer relationships within it. Why not just make explicit links?)
http://code.google.com/p/lambda4jdt/
It's quite an innovative idea. Just because the text is in java, why does it have to look like blub?
"Subtext takes the position that both text and diagrams have the same inherent limitation: they are paper-based media. Subtext represents programs as complex data structures that can not be fully printed out in a human-readable form. Text and diagrams are used to interactively present the program, not as a source encoding of it. This is the same approach taken by WYSIWYG applications like word processors and spreadsheets. Subtext is WYSIWYG programming."
http://en.wikipedia.org/wiki/Outliner
Not a new concept, but could certainly be applied way more often than it is.
Neat tool, but I really don't see this being that much of a time-saver over the built-in tools that Eclipse or Idea has for this.
In addition, using annotations to generate things like "cleanup" blocks around a stream seems dangerous in the long run to me - I don't like having any part of the flow of my code being handled by "magic".
I don't either, but I do agree with his argument that it will make code more readable. And, w.r.t. the auto-finally block thingy, badly implemented exception handling is a common cause of error in "maintenance mode" codebases. [Yes, code reviews SHOULD happen, etc.]
mountain.getLatitude() vs. dataClass.getDataElement("latitude");
It's the same reason I use XMLBeans (a schema-based codegen) for serializing/deserializing XML documents. Rather than writing mountainElement.getAttribute("latitude"), I can say mountain.getLatitude().
mountain.getDouble("latitude")
With at least two advantages: I'm pretty sure what getDouble will do, as opposed to getLatitude which may query google maps for all I know, and it can be processed in abstract ways. You can have operations which work on any structure: serialization, encription, logging, persistence, hash codes, sorting, raporting and a lot more.
Edit: It also makes for easier code reuse. You make something that works with mountains but could work just as well with houses, you don't have to play with class hierarchies to use it on both.
If you program like you are suggesting you lose the only reason to use java, and have to enforce your code using unit tests in a verbose language. No thanks.
The Scala example in the comments demonstrates that you can have your cake and eat it too:
case class Mountain (val name: String, val latitude: Int, val longtitude: Int, var country: String)
It's concise and type-safe, and can readily be used with more powerful aggregate constructs.
mountain.latitudesomething like:
(defdata mountain name :setter :getter latitude :getter etc ...)
might not be too hard to implement on top of clos (or on top of defstruct - but clos has the advantage of already having flexible accessors) not sure about clojure.
(defstruct mountain :name :latitude :longitude :country)
(struct mountain "Zugspitze" 47.416667 10.983333 "Germany")