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

Cas*_*ash 46 python programming-languages

Python是我目前所知道的最好的语言,但是由于自动完成,静态类型是一个很大的优势(尽管对动态语言的支持有限,但与静态支持相比,它没什么).我很好奇是否有任何语言尝试将Python的好处添加到静态类型语言中.特别是我对具有以下功能的语言感兴趣:

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

是否存在具有大量这些功能的静态类型语言?

Jør*_*ode 35

Boo是公共语言基础结构(又称Microsoft .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
Run Code Online (Sandbox Code Playgroud)

  • 嘘真的很好.语法支持与Python一样好或更好.功能支持是一流的.可以使用类型推断和鸭子类型.有一些闭包和宏提供了一种元编程形式.通常不能在运行时修改类,但是使用某些功能这不是问题 (2认同)

Man*_*ron 14

眼镜蛇是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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)


Nor*_*sey 9

虽然它不是面向对象的,但Haskell提供了许多您感兴趣的功能:

  • 语法支持列表推导,以及do各种排序/绑定结构的表示法.(对字典的语法支持仅限于对的列表,例如,

    dict = ofElements [("Sputnik", 1957), ("Apollo", 1969), ("Challenger", 1988)]
    
    Run Code Online (Sandbox Code Playgroud)
  • 函数使用元组类型支持完全闭包和多个返回值.不支持关键字参数,但"隐式参数"的强大功能有时可以替代.

  • 没有对类,类型或对象进行运行时修改.

  • 通过类型推断避免在任何地方具体化类/类型.

  • 使用Template Haskell进行元编程.

此外,只是让你有宾至如归的感觉,Haskell有很大的缩进!

我实际上认为Haskell与Python整体有着截然不同的感觉,但这主要是因为非常强大的静态类型系统.如果你有兴趣尝试使用静态类型的语言,那么Haskell就是目前最雄心勃勃的语言之一.


Luh*_*ann 8

它可能无法满足您的所有需求,但请查看Boo - CLI的腕式语言

如果你这样做,我强烈推荐Boo中的DSL:.NET中的领域特定语言,除了DSL方面,还包括一个非常好的附录中的Boo语法和大量的元编程.

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


dze*_*zen 5

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