类似于Python的静态类型语言有哪些?
我现在知道的最好的编程语言就是Python了,但静态类型的语言有一个大优点,就是可以自动补全(虽然动态语言也有一些支持,但远远比不上静态语言的支持)。我很好奇有没有什么语言能把Python的优点加到静态类型的语言里。特别是我对以下这些特性感兴趣:
- 语法支持:比如字典和数组推导式的语法
- 函数:关键字参数、闭包、元组/多个返回值
- 运行时修改或创建类
- 避免到处指定类(在Python中这是因为鸭子类型,虽然在静态类型语言中类型推断会更好)
- 元编程支持:在Python中通过反射、注解和 metaclass 来实现
有没有什么静态类型的语言具备这些特性呢?
12 个回答
11
虽然Haskell不是面向对象的语言,但它提供了很多你可能感兴趣的功能:
它支持列表推导的语法,还有
do
语法,可以用来处理各种顺序和绑定的结构。(不过,它对字典的支持比较有限,只能用成对的列表来表示,比如:dict = ofElements [("Sputnik", 1957), ("Apollo", 1969), ("Challenger", 1988)]
函数支持完整的闭包和使用元组类型的多个返回值。虽然不支持关键字参数,但有一个强大的“隐式参数”功能,有时可以替代它。
在运行时不能修改类、类型或对象。
通过类型推断,避免到处指定类或类型。
可以使用模板Haskell进行元编程。
另外,为了让你感到熟悉,Haskell对缩进的要求很严格!
我觉得Haskell和Python的感觉其实差别挺大的,主要是因为它有一个非常强大的静态类型系统。如果你对尝试静态类型语言感兴趣,Haskell是目前最有雄心的选择之一。
15
Cobra是一种静态类型的编程语言,适用于CLR(和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
36
Boo是一种静态类型的编程语言,主要用于公共语言基础设施,也就是微软的.NET平台。它的语法受到了Python的很大影响,并且在语法中包含了哈希、列表和数组的概念:
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