什么静态types的语言是类似于Python?

Python是目前我所知道的最好的语言,但是由于自动完成,静态types是一个很大的优势(虽然对dynamic语言的支持有限,但与静态types相比并不算什么)。 我很好奇,如果有任何语言试图将Python的好处添加到静态types的语言。 特别是我在有以下function的语言中很有趣:

  • 语法支持:如字典,数组理解
  • 函数:关键字参数,闭包,元组/多个返回值
  • 运行时修改/创build类
  • 避免在任何地方指定类(在Python中,这是由于鸭子键入,尽pipetypes推断在静态types语言中会更好)
  • 元编程支持:这是通过reflection,注释和元类在Python中实现的

是否有任何静态types的语言与这些function的相当数量?

Boo是Common Language Infrastructure(也就是Microsoft .NET平台)的静态types语言。 语法的灵感来自于Python,而hashes / lists / array是语法的一部分:

i = 5 if i > 5: print "i is greater than 5." else: print "i is less than or equal to 5." hash = {'a': 1, 'b': 2, 'monkey': 3, 42: 'the answer'} print hash['a'] print hash[42] for item in hash: print item.Key, '=>', item.Value 

眼镜蛇是CLR的静态types语言(如Boo)。 从其网页上:

Cobra是一种通用编程语言,具有:

  - a clean, high-level syntax - static and dynamic binding - first class support for unit tests and contracts - compiled performance with scripting conveniences - lambdas and closures - extensions and mixins - ...and more 
 Sample code: """ This is a doc string for the whole module. """ class Person """ This is a class declaration. """ var _name as String # declare an object variable. every instance of Person will have a name var _age as int cue init(name as String, age as int) _name = name _age = age def sayHello # This is a method # In strings, anything in brackets ([]) is evaluated as an expression, # converted to a string and substituted into the string: print 'Hello. My name is [_name] and I am [_age].' def add(i as int, j as int) as int """ Adds the two arguments and returns their sum. """ return i + j 

虽然它不是面向对象的,但Haskell提供了大量您感兴趣的function:

  • 列表parsing的语法支持,以及各种sorting/绑定结构的标记。 (对字典的语法支持仅限于对的列表,例如,

     dict = ofElements [("Sputnik", 1957), ("Apollo", 1969), ("Challenger", 1988)] 
  • 函数支持使用元组types的完全闭包和多个返回值。 关键字参数不被支持,但是“隐式参数”的强大特征有时可以替代。

  • 没有运行时修改类,types或对象。

  • 通过types推断避免所有types/types。

  • 使用模板Haskell进行元编程。

而且,就这样你会觉得在家,Haskell有显着的缩进!

实际上,我认为Haskell与Python的整体感觉有很大的不同,但主要是因为静态types系统非常强大。 如果你有兴趣尝试一种静态types的语言,Haskell是目前最雄心勃勃的语言之一。

它可能不符合您的所有需求,但看看Boo – CLI的腕表语言

如果是这样的话,我强烈build议使用Boo的DSL:.NET中的特定领域语言 ,除了DSL方面外,还包括一个很好的附录中的Boo语法以及大量的元编程。

此外, 教程是一个很好的资源。

Go编程语言。 我见过一些类似的范例。

Rpython是静态types的Python的一个子集。

D编程语言是一种静态types的本地编译语言,它有一些Python启发的重要特性。

该语言内置了数组和关联数组。 没有列表parsing,但std.range和std.algorithm库填充了大部分void。 例如,下面是一个总结D中所有从0到100的偶数的方法:

 auto result = reduce!"a + b"( filter!"a % 2 == 0"( iota(0, 100) ) ); 

到目前为止,没有关键字参数,但closures是存在的。 元组受支持,但不能自动解包。

在D中,避免在每个地方使用auto关键字和模板指定类(以及一般types)。 例如,这里是通用代码来查找任何数字types的数组的产品:

 // The return type of product() is inferred. auto product(T)(T[] array) { T ret = 1; foreach(num; array) { // typeof(num) is inferred. ret *= num; } return ret; } 

D的元编程支持包括编译时自省(例如,您可以在编译时遍历类或结构的字段),运行时types信息以及实际为超出简单generics的元编程devise的模板。 例如,下面是如何编写一个通用函数来为两个结构生成一个默认的比较操作,如果您需要像二叉树那样的任意总sorting,这是非常有用的:

 /**Returns -1 if lhs < rhs, 0 if lhs == rhs, 1 if lhs > rhs.*/ int compareStructs(T)(T lhs, T rhs) { foreach(tupleIndex, value; lhs.tupleof) { if(value < rhs.tupeof[tupleIndex]) { return -1; } else if(value > rhs.tupleof[tupleIndex]) { return 1; } } return 0; } 

在dynamictypes语言中,自动完成仍然是可能的。 没有任何东西阻止IDE进行types推断或检查,即使语言实现没有。

如果自动完成是你正在寻找的东西,那么你可能想要坚持使用Python,而不是使用一个好的IDE。

尝试PyCharm: http : //www.jetbrains.com/pycharm/index.html

除非你编写了一些非常dynamic的东西(无论如何你都不能用静态语言来编写),否则它会跟上代码,给你完成,重构和我们习惯静态types语言的所有其他东西。

您可以通过执行操作,在IDE中input提示信息:

 def foo(bar): if 0: bar = Bar() # "if 0" will be removed from the bytecode automatically by python bar. # will now autocomplete 

我认为Eric和PyScripter在Windows上有很好的自动完成function,但是对于Visual Studio(Express)来说可能不如PyTools。

对于Python中的静态types,我会使用Cython: http ://docs.cython.org/src/quickstart/cythonize.html