[Python-bugs-list] [ python-Feature Requests-778763 ] Optional type enforcement

SourceForge.net noreply@sourceforge.net
Sun, 27 Jul 2003 23:18:59 -0700


Feature Requests item #778763, was opened at 2003-07-28 01:18
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=778763&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Matt Perry (occupant4)
Assigned to: Nobody/Anonymous (nobody)
Summary: Optional type enforcement

Initial Comment:
One of the things that makes Python easy to code in is
the fact that you don't have to explicitly declare the
types of variables, or associate a specific type with a
variable.  However, this can potentially lead to
somewhat confusing and hard-to-maintain code, not to
mention make it easier to introduce bugs.

An idea struck me that it might be possible to combine
the power and safety of a strictly type-checked
language with the speed and ease of coding of Python. 
Normal variables as they stand now would be unaffected
by this feature, and retain their polymorphic type
capabilities.  Allow the programmer to optionally
declare the type of a variable somehow, be it with the
C-like syntax "int x", or Pascal's (I think?) "x: int",
or a new syntactic representation.  Python could then
issue a TypeError if the program attempts to assign a
non-int to x.  Obviously this feature could apply to
any data type, such as string, list, or perhaps
user-defined classes.

Class member variables can be declared as now in the
class definition body, with the same syntax to specify
a type, ie:
   class Test:
       int x = 5
       string name = "test"
       untyped = None

In addition, adding a type specifier to a function's
parameters could serve another means of distinguishing
it from other functions, which could allow function
overloading, ie:
   def double(int x):
       return 2*x
   def double(string x):
       return x + ' ' + x

Advantages:
- clarifies the author's intention in writing code, and
could help prevent confusion as to what type a
variable/parameter is.
- helps prevent bugs due to type mistakes
- doesn't interfere with existing code or coding style,
as dynamically typed variables are still allowed
- could allow function overloading if it was wanted

Disadvantages:
- implementing a type checker could be difficult?
- potentially pollutes namespace with extra keywords
like 'string' or 'list'.  may need to choose different
keywords.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=778763&group_id=5470