Ronald Adam
Posts: 5
Nickname: ron100
Registered: Mar, 2005
|
|
Re: Python Optional Typechecking Redux
|
Posted: Mar 15, 2005 2:53 PM
|
|
I'm probably suggesting something that's already been ruled out sometime ago, if so, please disregard this if it's been voted out already.
I think having the option to use types constraints optional or on the fly is a good idea and can further pythons use in areas that require higher confidence.
If the following can be considered true:( Now or in the future. )
* Every object knows it's own type. * Some objects may know how to convert it's self to another type if asked to. * Every name in name space has a type preference associated with it, which may be 'none' or 'any'. * Then when ever an object is attached to a name, the name type preference must match the objects type. * If in the case they do not match, then the object is asked to change if it knows how. * If it can't change, then a 'unmatched type' error is produced.
So considering the above, the name is created with a type preference and assigned to an object at the same time.
a:int = 1
I'm not sure I care for the use of : and -> symbols. They are short and concise, but also can be confusing. I would prefer the use of something more readable.
a of 'int' = 1
name [of 'type'] = object
where the [of 'type'] is optional and has the meaning of:
"From now on, this name can only be pointed to objects of this type."
I know this creates new syntax, but I feel 'type' is a very fundamental concept, so do you want it out front and explicit, or do you want to combine it within other syntax, which I feel makes it less readable and harder to implement.
Having the type specified as a string has some advantages that we can have many types.
class point3d: def __init__( self ): self.x = 0 self.y = 0 self.z = 0
efg of 'point3d' = point3d()
In functions, I think having the variables in the function reassigned to types is enough. Using a:'int' might be considered as a way to give names a preference in advance.
def xyz( a, b): a of 'int' = a # Reassign with type constraint b of 'int' = b # could cause conversion if not the # same, or an error message. c = a * b**2 return c
Would be the same as the above, and might be a little faster:
def xyz( a:'int', b:'int'): c = a * b**2 return c
The returned value is typed checked when it is given to receiving name and object, where we need it.
a = 5 b = 2 result of 'int' = xyz( a, b)
The name type preferences in this way could be reassigned, so it doesn't force, tie. optional, the types.
a of 'int' = 5 a of 'float' = 5.3 a = 3 # conversion takes place due to type mismatch. a = 'woah' # generates an 'type mismatch' error a of 'none' = 'anything' # remove a type preference
And for exceptions:
try: a of 'outerspace' = underwater() except: print 'Deploy lifeboats!'
I'm not proficient enough to know how the insides should work, so this may be totally unworkable. But maybe there's some good ideas or concepts.
Would it be possible subdivided name space into sets of 'types' as a way to give them 'type' preferences without causing a performance hit?
Done thinking out loud, Ron
|
|