views:

549

answers:

8

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?

+20  A: 

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
Jørn Schou-Rode
+1 for the relevant example. Nice answer.
Luhmann
Boo is really nice. Syntactic support is as good or better than Python. Function support is first class. Type inference and duck typing are available. There are closures and macros provide for a form of meta programming. Classes in general can't be modified at runtime, but with some of the features this is less of an issue
Casebash
+7  A: 

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.

Luhmann
A: 

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.

Andrew McGregor
The autocompletion in dynamic languages is nothing compared to static
Casebash
Exactly. Tools like PyDev are nice but the autocomplete/intellisense is awful. When it works it's far too slow.
Finglas
The autocompletion needed for dynamic languages is nothing compared to static.
kaizer.se
Autocompletion is impossible _in general_ in dynamically-typed languages. And it's undesirable, too. The whole advantage of dynamic typing is that it is often convenient to write code that cannot be type-checked. If you're going to write a program that *can* be type-checked automatically, you might as well use static typing. Hence this question.
Porculus
+2  A: 

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
truppo
Anyone know how PyCharm compares to Eclipse Python support? I'll look at it, but I'll still be surprised if it is even close to statically typed languages
Casebash
I find it way better than PyDev atleast. But, I switched to PyCharm about a year ago, so my opinions of PyDev is a bit outdated.
truppo
How does if 0 provide a type hint?
Casebash
The typehint is "bar = Bar()", the "if 0" is to make python not use that line (since we want bar to be the value passed in). When using "if 0" python will remove that line completely from the bytecode and there will be no performance penalty at all.
truppo
The fact that you have to go through this hackery shows how Python's IDEs could be improved
prestomation
@prestomation: And how would that be? If I write a function, how on earth should the IDE know what type the arguments are? Not until I have actually begun using the function elsewhere in my code can it make an educated guess. Until then some kind of type hint is needed.
truppo
+5  A: 

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
Manuel Ceron
+1  A: 

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;
}
dsimcha
+5  A: 

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.

Norman Ramsey
+1  A: 

try the Go programming language ? I've seen some similar paradigm.

dzen
Go does have some nice syntactical constructs, but as far as I know it doesn't have many of the other features
Casebash