What statically typed languages are similar to Python? [closed]
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?
Solution 1:
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
Solution 2:
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
Solution 3:
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.