4 ms·
People interested in optional types for Python may be interested in my @typ decorator that implements them: https://github.com/cabalamat/ulib/blob/master/debugd
by cabalamat 13y ago
People interested in optional types for Python may be interested in my @typ decorator that implements them: https://github.com/cabalamat/ulib/blob/master/debugdec.md https://github.com/cabalamat/ulib/blob/master/debugdec.md
Sample:
@typ((int,float), ret=(int,float))
def square(x):
return x*x
Here the parameter x is an int or float, and so is the return type.
It doesn't currently let you compose types, but that wouldn't be too difficult to add, e.g. {str:int} could mean a dictionary whose keys are strings and values are integers.
- acjohnson55 13y agoThat's okay, but that doesn't seem to help much to verify your type correctness before runtime, which seems to me to be a big part of the benefit of type checking. Also, what you'd really want is something that implements type variables like: @typ[A implements *](A, ret=A) def square(x): return x * x But I don't know how you'd do that in Python. BTW, your printargs looks pretty cool! I might just makes something like that for JS...
- cabalamat 13y ago> that doesn't seem to help much to verify your type correctness before runtime I don't think that's possible in Python. > Also, what you'd really want is something that implements type variables That is possible, you could check what the type of the incoming parameter is and then check that the return value is the same type. I'm not sure that there's an obvious syntax for saying this, but one could always send a string to @typ and have it parse some made-up syntax. I suspect this would be a lot of effort and it would probably make sens to use a typed language, instead of trying to shoehorn it into Python. I invented @typ so I could quickly document the types in my functions, & with the added advantage that it catches some errors. > your printargs looks pretty cool! I might just makes something like that for JS... I look forward to seeing it.