刚才写代码的时候又是在不停查文档,甚是心烦。一怒,拿出
IronPython,类似这样:
IronPython 2.6 Beta DEBUG (2.6.0.1) on .NET 2.0.50727.3082
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference('System')
>>> import System
>>> System
<module 'System' (CLS module, 2 assemblies loaded)>
>>> R = System.Text.RegularExpressions.Regex
>>> r = R('abc')
>>> s = '123abcd456'
>>> dir(r)
['CacheSize', 'CompileToAssembly', 'Equals', 'Escape', 'GetGroupNames', 'GetGroupNumbers', 'GetHashCode', 'GetObjectData', 'GetType', 'GroupNameFromNumber', 'GroupNumberFromName', 'InitializeReferences', 'IsMatch', 'Match', 'Matches', 'MemberwiseClone', 'Options', 'ReferenceEquals', 'Replace', 'RightToLeft', 'Split', 'ToString', 'Unescape', 'UseOptionC', 'UseOptionR', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__subclasshook__', 'capnames', 'caps', 'capsize', 'capslist', 'factory', 'pattern', 'roptions']
>>> m = r.Match(s)
>>> dir(m)
['Captures', 'Empty', 'Equals', 'GetHashCode', 'GetType', 'Groups', 'Index', 'Length', 'MemberwiseClone', 'NextMatch', 'ReferenceEquals', 'Result', 'Success', 'Synchronized', 'ToString', 'Value', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__subclasshook__']
>>> m.Success
True
>>> m.Index
3
>>> m.ToString()
'abc'
>>>
我可不想为了印证记忆中对.NET的正则表达式的模糊印象而去写C#代码->编译->运行->发现错误->修改->再编译->……这种时候用IronPython正顺手。
使用IronPython时,打开.NET大门的方法就是神奇的
import clr。接下来通过clr对象来添加.NET程序集的引用,就可以开始用.NET的类库了(mscorlib是默认引入的)。整个使用体验相当流畅,刚启动解释器的时候比较卡的时间也减少了(虽然还是比CPython刚启动时慢,没办法)。
=====================================================================
我这IronPython是从
CodePlex的
DLR项目编译出来的,同一个Debug目录下还有
IronRuby 0.4.0.0,于是干脆用IronRuby也试一次:
IronRuby 0.4.0.0 on .NET 2.0.50727.3082
Copyright (c) Microsoft Corporation. All rights reserved.
>>> require 'mscorlib'
=> true
>>> System::Text::RegularExpressions
=> System::Text::RegularExpressions
>>> R = System::Text::RegularExpressions::Regex
=> System::Text::RegularExpressions::Regex
>>> r = R.new 'abc'
=> abc
>>> s = '123abcd456'
=> "123abcd456"
>>> r.methods.sort
=> ["==", "===", "=~", "__id__", "__send__", "class", "clone", "display", "dup", "eql?", "equal?", "extend", "freeze", "frozen?", "hash", "id", "inspect", "instance_eval", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "kind_of?", "method", "methods", "nil?", "object_id", "private_methods", "protected_methods", "public_methods", "respond_to?", "send", "singleton_methods", "taint", "tainted?", "to_a", "to_s", "type", "untaint"]
>>> r.class
=> System::Text::RegularExpressions::Regex
>>> m = r.match s
=> abc
>>> m.class
=> System::Text::RegularExpressions::Match
>>> m.methods.sort
=> ["==", "===", "=~", "__id__", "__send__", "class", "clone", "display", "dup", "eql?", "equal?", "extend", "freeze", "frozen?", "hash", "id", "inspect", "instance_eval", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "kind_of?", "method", "methods", "nil?", "object_id", "private_methods", "protected_methods", "public_methods", "respond_to?", "send", "singleton_methods", "taint", "tainted?", "to_a", "to_s", "type", "untaint"]
>>> m.index
=> 3
>>> m.length
=> 3
>>> m.success
=> true
>>> m.to_s
=> "abc"
>>>
Well……看来不太对劲。虽然程序集也正确引入了,类型和方法也都正确加载执行了,但methods返回的方法列表里没有.NET的方法。这真够郁闷的,以前的版本明明可以的啊。好一段时间没玩IronRuby,这.NET interop怎么好像退化了 OTL
Anyway。使用IronRuby时,打开.NET大门的神奇方法是
require 'mscorlib'。然后可以用强名称来引入别的.NET程序集,而标准库里的一些程序集是默认引入的,例如System.dll,所以上面我没有显式引入这个程序集。然后.NET命名空间要跟随Ruby代码习惯用::来分隔,基本上没别的大问题了。需要使用.NET的可变长度参数的方法时,可以对Ruby数组调用to_clr_array方法,跟JRuby非常类似。
=====================================================================
就目前而言,IronPython的完成度远在IronRuby之上。如果你也想现在就用Iron-*语言来探索.NET的话,我会推荐IronPython。不过IronPython 2.6现在还是暂时别用,bug多多……过不久等2.6出正式版之后再用吧~现在用
IronPython 2.0.1稳定。
至于一个不是出自微软的Iron-*语言,IronScheme,它使用的DLR是非常老的版本,性能肯定是比不过微软的两个Iron-*语言。不过就其对R6RS的实现来说,它还是值得一试的,毕竟“实现了什么语义”比起“如何实现”更值得一般程序员所关注。