哪些静态类型语言与 Python 相似?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2264889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:12:44  来源:igfitidea点击:

What statically typed languages are similar to Python?

pythonprogramming-languages

提问by Casebash

Python is the nicest language I currently know of, but static typing is a big advantage due to auto-completion (although there is limited support for dynamic languages, it is nothing compared to that supported in static). I'm curious if there are any languages which try to add the benefits of Python to a statically typed language. In particular I'm interesting in languages with features like:

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

  • Syntax support: such as that for dictionaries, array comprehensions
  • Functions: Keyword arguments, closures, tuple/multiple return values
  • Runtime modification/creation of classes
  • Avoidance of specifying classes everywhere (in Python this is due to duck typing, although type inference would work better in a statically typed language)
  • Metaprogramming support: This is achieved in Python through reflection, annotations and metaclasses
  • 语法支持:例如字典、数组推导式
  • 函数:关键字参数、闭包、元组/多个返回值
  • 运行时修改/创建类
  • 避免在任何地方指定类(在 Python 中这是由于鸭子类型,尽管类型推断在静态类型语言中效果更好)
  • 元编程支持:这是在 Python 中通过反射、注释和元类实现的

Are there any statically typed languages with a significant number of these features?

是否有任何静态类型语言具有大量这些功能?

采纳答案by J?rn Schou-Rode

Boois a statically typed language for the Common Language Infrastructure (aka. the Microsoft .NET platform). The syntax is highlyinspired by Python, and hashes/lists/array are part of the syntax:

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

回答by Manuel Ceron

Cobrais a statically typed language for the CLR (as Boo). From its web page:

Cobra是 CLR 的静态类型语言(作为 Boo)。从它的网页:

Cobra is a general purpose programming language with:

 - 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

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

回答by Norman Ramsey

Although it is not object-oriented, Haskelloffers a significant number of the features that interest you:

尽管它不是面向对象的,但Haskell提供了大量您感兴趣的特性:

  • Syntax support for list comprehensions, plus donotation for a wide variety of sequencing/binding constructs. (Syntax support for dictionaries is limited to lists of pairs, e.g,

    dict = ofElements [("Sputnik", 1957), ("Apollo", 1969), ("Challenger", 1988)]
    
  • Functions support full closures and multiple return values using tuple types. Keyword arguments are not supported but a powerful feature of "implicit arguments" can sometimes substitute.

  • No runtime modification of classes, types or objects.

  • Avoidance of specificying classes/types everywhere through type inference.

  • Metaprogramming using Template Haskell.

  • 对列表推导式的语法支持,以及do各种测序/绑定结构的符号。(对字典的语法支持仅限于成对列表,例如,

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

  • 没有对类、类型或对象的运行时修改。

  • 通过类型推断避免在任何地方指定类/类型。

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

Also, just so you will feel at home, Haskell has significant indentation!

此外,为了让您有宾至如归的感觉,Haskell 具有显着的缩进!

I actually think Haskell has quite a different feel from Python overall, but that is primarily because of the extremely powerful static type system. If you are interested in trying a statically typed language, Haskell is one of the most ambitious ones out there right now.

实际上,我认为 Haskell 总体上与 Python 有很大不同,但这主要是因为极其强大的静态类型系统。如果你有兴趣尝试一种静态类型语言,Haskell 是目前最雄心勃勃的语言之一。

回答by Luhmann

It may not match all your needs, but have a look at Boo - The wristfriendly language for the CLI

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

If you do, I highly recommend DSLs in Boo: Domain-Specific Languages in .NETwhich apart from the DSL aspects, covers Boo syntax in a very nice appendix and a lot of meta-programming.

如果你这样做,我强烈推荐DSLs in Boo: Domain-Specific Languages in .NET除了 DSL 方面,它在一个非常好的附录中涵盖了 Boo 语法和许多元编程。

Furthermore the tutorialsare a great resource.

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

回答by dzen

The Go programming language. I've seen some similar paradigm.

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

回答by Chirag

Rpython is a subset of Python that is statically typed.

Rpython 是静态类型的 Python 的子集。

回答by dsimcha

The D programming language is a statically typed, natively compiled language that has some significant features inspired by Python.

D 编程语言是一种静态类型、本机编译的语言,它具有一些受 Python 启发的重要特性。

Arrays and associative arrays are built into the language. There are no list comprehensions, but the std.range and std.algorithm libraries fill much of that void. For example, here's a way to sum up all the even numbers from 0 to 100 in D:

数组和关联数组内置于语言中。没有列表推导式,但 std.range 和 std.algorithm 库填补了大部分空白。例如,这里有一种方法可以将 D 中从 0 到 100 的所有偶数相加:

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

There are no keyword arguments so far, but closures are there. Tuples are supported, but not unpacked automatically.

到目前为止还没有关键字参数,但是有闭包。支持元组,但不会自动解包。

In D, you avoid specifying classes (and types in general) everywhere with the autokeyword and with templates. For example, here is generic code to find the product of array of any numeric type:

在 D 中,您避免在任何地方使用auto关键字和模板指定类(和一般类型)。例如,这里是查找任何数字类型数组的乘积的通用代码:

// 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's metaprogramming support consists of compile time introspection (for example, you can iterate over the fields of a class or struct at compile time), runtime type information, and templates that are actually designed for metaprogramming beyond simple generics. For example, here's how to write a generic function that generates a default comparison operation for two structs, which is useful if you need an arbitrary total ordering for something like a binary tree:

D 的元编程支持包括编译时内省(例如,您可以在编译时迭代类或结构的字段)、运行时类型信息和实际上为元编程设计的模板,而不是简单的泛型。例如,这里是如何编写一个通用函数来为两个结构生成默认比较操作,如果您需要对二叉树之类的东西进行任意总排序,这很有用:

/**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;
}

回答by Andrew McGregor

Autocompletion is still possible in a dynamically typed language; nothing prevents the IDE from doing type inference or inspection, even if the language implementation doesn't.

在动态类型语言中,自动完成仍然是可能的;没有什么可以阻止 IDE 进行类型推断或检查,即使语言实现没有。

回答by truppo

If auto-completion is the thing you are looking for, then you might wanna stick with Python and use a great IDE instead.

如果自动完成是您正在寻找的东西,那么您可能想坚持使用 Python 并改用出色的 IDE。

Try PyCharm: http://www.jetbrains.com/pycharm/index.html

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

Unless you are coding some extremely dynamic stuff (which you can't probably do in a static language anyway), it will keep up with the code and give you completion, refactoring and all the other goodies we are used to in statically typed languages.

除非你正在编写一些非常动态的东西(无论如何你不可能用静态语言来做),它会跟上代码并为你提供完成、重构和我们在静态类型语言中习惯的所有其他好处。

You can give typehints to the IDE where you really need it by doing:

您可以通过执行以下操作为您真正需要的 IDE 提供类型提示:

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

回答by Aardappel

Lobster (http://strlen.com/lobster/) is a statically typed programming language with Python-esque syntax.

Lobster ( http://strlen.com/lobster/) 是一种具有 Python 式语法的静态类型编程语言。

It has a few things you're asking for:

它有一些你要求的东西:

  • Type inference, so your code can look similar to Python without having to specify types everywhere. In fact, it goes further with type inference than languages like Haskell.
  • Closures: they are syntactically lighter than Python (create your own control structures) and yet more powerful (may be multi-line, you can return from them to enclosing functions).
  • Multiple return values.
  • 类型推断,因此您的代码可以看起来类似于 Python,而无需在任何地方指定类型。事实上,它在类型推断方面比 Haskell 等语言更进一步。
  • 闭包:它们在语法上比 Python 更轻(创建你自己的控制结构)但更强大(可能是多行的,你可以从它们返回到封闭函数)。
  • 多个返回值。

It doesn't do so well on these items:

它在这些项目上表现不佳:

  • Syntax for dictionaries or array comprehensions. Though its syntax for map/filter is so minimal that it can probably compete with array comprehensions.
  • Keyworded arguments currently only for constructors.
  • Runtime modification of classes: nope.. its a pretty static language.
  • Reflection: also nope, though this would certainly be possible.
  • 字典或数组推导式的语法。尽管它的 map/filter 语法非常小,以至于它可能会与数组推导式竞争。
  • 目前仅用于构造函数的关键字参数。
  • 类的运行时修改:不......它是一种非常静态的语言。
  • 反思:也没有,虽然这肯定是可能的。