What statically typed languages are similar to Python?

Question:

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:

  • 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

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

Asked By: Casebash

||

Answers:

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

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
Answered By: Jørn Schou-Rode

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

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

Furthermore the tutorials are a great resource.

Answered By: Luhmann

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.

Answered By: Andrew McGregor

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

Try 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:

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

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

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
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
Answered By: Manuel Ceron

The D programming language is a statically typed, natively compiled language that has some significant features inspired by 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:

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 auto keyword and with templates. For example, here is generic code to find the product of array of any numeric type:

// 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:

/**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;
}
Answered By: dsimcha

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

  • Syntax support for list comprehensions, plus do notation 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.

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

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.

Answered By: Norman Ramsey

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

Answered By: dzen

I think Eric and PyScripter have nice autocompletion on Windows, but maybe not as good as PyTools for Visual Studio (Express).

For static typing in Python, i’d use Cython: http://docs.cython.org/src/quickstart/cythonize.html

Answered By: Cees Timmerman

Rpython is a subset of Python that is statically typed.

Answered By: Chirag

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

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.

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.
Answered By: Aardappel

I know this is an old thread, but I think it’s worth mentioning Nim, that has reached version 1.0 recently and one of its best characteristics is it’s metaprogramming capabilities.
And a lot more.

https://nim-lang.org/

Answered By: Albus70007
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.