Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
来源:互联网 发布:linux 管道符什么作用 编辑:程序博客网 时间:2024/05/16 01:36
Posted by: Joseph Ottinger on ?? 18, 2008 DIGG<script type="text/javascript">function _parseDiggURL() { var _diggURL = "http://digg.com/submit?phase=2&topic=Programming&url=" _diggURL += encodeURIComponent(location.href); _diggURL += "&title="; _diggURL += escape ( "Typesafe DSLs in Java: Part 1 — Typesafe Bytecode") ; _diggURL += "&bodytext="; _diggURL += escape ( ""<a href="http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/">Typesafe DSLs in Java: Part 1 — Typesafe Bytecode</a>," by Jevgeni Kabanov (M. Sc., in case you're interested), offers a way to use ASM to build a typesafe engineering DSL as a case study. There's also a followup ("<a href="http://dow.ngra.de/2008/03/26/typesafe-asm-problems-solved/">Typesafe ASM — problems solved?</a>") that addresses some of the problems the first post left unaddressed. Jevgeni is scheduled to be speaking at <a href="http://javasymposium.techtarget.com/europe/index.html">TSSJS in Prague</a>, although he's not on the schedule yet - it looks like a cool session based on the abstract.<br><br>It's an interesting concept - one of the things that Jevgeni is addressing in his post is the following sort of process, which is an example of a normal fluent interface:<pre>Person p=new Person().setFirstName("Jean-Claude").setHair(Hair.BROWN).setHair(Hair.GREY);</pre>In this case, you can see where setting the hair color is done twice, which Jevgeni addresses by saying:<blockquote>The type you return from your DSL method should allow exactly those operations that are possible with the current state.</blockquote>In other words, the <code>setHair</code> method shouldn't return a Person, but a Person implementation for which setHair() isn't valid - which means the above code wouldn't compile.<br><br>The cool thing here is that Jevgeni's actually producing something to address this sort of problem - in that fluent interfaces are a result of not having good DSLs in the first place. <br><br>There's a lot of potential here. The idea is that with typesafe fluent interfaces, you can build a domain-specific type of language for Java that are just as powerful as you might find in Scala or Haskell. <br><br>There's even a project in the works to show all of this in action: <a href="http://code.google.com/p/flu/">flu</a>, hosted on Google Code. It's important: <b>this project is not production-ready</b>, although Jevgeni says that it will be eventually.<br><br>Some example code using this concept:<pre>Person p = new Person();<br><br>List&lt;Tuple3&gt;&lt;String, Integer, Date&gt;&gt; persons =<br> new QueryBuilder(datasource)<br> .from(p)<br> .select(p.name, p.height, p.birthday)<br> .where(gt(p.height, 170))<br> .find();<br>for (Tuple3&lt;String, Integer, Date&gt; person : persons) {<br> String name = person.v1();<br> Integer height = person.v2();<br> Date birthday = person.v3();<br> System.out.println(<br> name + " " + height + " " + birthday); <br>}</pre>This is cool stuff - even if the SQL parsing isn't directly useful to you, the potential for enforcing type safety and valid operations on fluent interfaces can be a powerful addition to the toolbelt.").substring(0,300); return _diggURL ;}</script>
"Typesafe DSLs in Java: Part 1 — Typesafe Bytecode," by Jevgeni Kabanov (M. Sc., in case you're interested), offers a way to use ASM to build a typesafe engineering DSL as a case study. There's also a followup ("Typesafe ASM — problems solved?") that addresses some of the problems the first post left unaddressed. Jevgeni is scheduled to be speaking at TSSJS in Prague, although he's not on the schedule yet - it looks like a cool session based on the abstract.It's an interesting concept - one of the things that Jevgeni is addressing in his post is the following sort of process, which is an example of a normal fluent interface:
Person p=new Person().setFirstName("Jean-Claude").setHair(Hair.BROWN).setHair(Hair.GREY);In this case, you can see where setting the hair color is done twice, which Jevgeni addresses by saying:
The type you return from your DSL method should allow exactly those operations that are possible with the current state.In other words, the
setHair
method shouldn't return a Person, but a Person implementation for which setHair() isn't valid - which means the above code wouldn't compile.The cool thing here is that Jevgeni's actually producing something to address this sort of problem - in that fluent interfaces are a result of not having good DSLs in the first place.
There's a lot of potential here. The idea is that with typesafe fluent interfaces, you can build a domain-specific type of language for Java that are just as powerful as you might find in Scala or Haskell.
There's even a project in the works to show all of this in action: flu, hosted on Google Code. It's important: this project is not production-ready, although Jevgeni says that it will be eventually.
Some example code using this concept:
Person p = new Person();This is cool stuff - even if the SQL parsing isn't directly useful to you, the potential for enforcing type safety and valid operations on fluent interfaces can be a powerful addition to the toolbelt.
List<Tuple3><String, Integer, Date>> persons =
new QueryBuilder(datasource)
.from(p)
.select(p.name, p.height, p.birthday)
.where(gt(p.height, 170))
.find();
for (Tuple3<String, Integer, Date> person : persons) {
String name = person.v1();
Integer height = person.v2();
Date birthday = person.v3();
System.out.println(
name + " " + height + " " + birthday);
}
Re: Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
Posted by: Jevgeni Kabanov on ?? 18, 2008 in response to Message #250787
To give credit where credit is due -- the typesafe SQL stuff is joint work with Juhan Aasaru and Rein Raudjärv. Re: Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
Posted by: Cedric Beust on ?? 18, 2008 in response to Message #250787
p=new Person().setFirstName("Jean-Claude").setHair(Hair.BROWN).setHair(Hair.GREY);In this case, you can see where setting the hair color is done twice, which Jevgeni addresses by saying:The type you return from your DSL method should allow exactly those operations that are possible with the current state.In other words, thesetHair
method shouldn't return a Person, but a Person implementation for which setHair() isn't valid - which means the above code wouldn't compile.
Really? Is this possible? (I haven't read the article yet. The formatting is painful and I don't like to read code that forces me to scroll back and forth horizontally all the time...)
It seems to me that the only way to know that setHair() is no longer valid is to run code inside that object, which therefore needs to be compiled.
I can't really see how you could discover this kind of error at compile time...
--
Cedric
- Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
- Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
- Java Tip 27: Typesafe constants in C++ and Java
- 译 Programming with typesafe enums and annotations in Java 5
- TypeSafe config & HOCON + Read properties in java (with example)
- JAVA 配置管理库 typesafe.config
- typesafe activator
- 通过Typesafe Activator创建akka java sample
- Java ByteCode Part 1
- Java基础学习总结(66)——配置管理库typesafe.config教程
- Typesafe Activator 入门使用
- Spring框架创始人加入Typesafe
- 试用配置管理库typesafe.config
- Java ByteCode Part 2
- com.typesafe.config加载任意位置的配置文件
- com/typesafe/config/ConfigException : Unsupported major.minor version
- Java bytecode
- Java bytecode
- shell+sed+awk和perl和python的区别
- flex的ResourceBundle用处
- 关于getDefinitionByName的问题
- 呵呵 今天似乎又有进步了
- CAN简述
- Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
- 水饺-水饺-水饺
- Abstract Factory Design Pattern for Dependency Injection
- Autodesk面试技术题解答(zz)
- 取得运行程序的绝对路径名
- vim中删除除当前buffer外所有的buffer
- Hibernate 对Dao层的处理实例
- jsf big lost
- Java正则表达式:让分组group()代替replaceAll吧!