Optional Static Typing
moma
moma at example.net
Sat Dec 25 09:46:33 EST 2004
bearophileHUGS at lycos.com wrote:
> Adding Optional Static Typing to Python looks like a quite complex
> thing, but useful too:
> http://www.artima.com/weblogs/viewpost.jsp?thread=85551
>
> I have just a couple of notes:
>
> Boo (http://boo.codehaus.org/) is a different language, but I like its
> "as" instead of ":" and "->", to have:
> def min(a as iterable(T)) as T:
> Instead of:
> def min(a: iterable(T)) -> T:
>
I want to introduce a shorter syntax form:
Declare variables
a'int
a'int = 13
s'string = "Santana"
d'float
def min(a'int, b'int)'int:
c'int # Declare a local variable c of type int
c = a
...
*************************************
The (template) notation is very useful.
def min(a'T, b'T)'T:
c'T
c = a
....
f'float = min(1.2, 2.2)
i'int = min(9, f) ## of course: comiler adds int(f) type conversion
*************************************
But these 2 should be syntactically wrong. The type of T is not obvious.
def max(a'int, b'int)'T:
....
def max(a, b)'T:
....
*************************************
The big question is how to handle combound data types (container
objects) ? lists, tuples, maps...
Can a list contain various data types?
>>> h=['abc', 13, (9,8)]
# Declare h as list of ints
h'int[] = [1, 8, 991]
# These declarations produce syntax errors
h'int = [1, 8, 991]
error: h is a scalar not container
h'int[] = ['abc', 13, (9,8)]
^^
error: expecting int value
*************************************
Tuples
A general sequence
t = 1, 3, 4,
A tuple of ints
t'int() = 1, 3, 4,
What about this?
u'int() = t, 6, 7,
Yes, it's OK. because the basic_scalar_values are ALL ints.
>>>print u
((1,3,4), 6,7)
Maps
.....
*************************************
I think the compiler should allow typeless containers even you compile
with --strict option. Apply --strict (strictly) to scalar types only.
*************************************
class A:
pass
def func1(h'A)
# Expects (instance) A or any subclass of A
....
*************************************
// moma
http://www.futuredesktop.org/OpenOffice.html
More information about the Python-list
mailing list