andremm / typedlua

An Optional Type System for Lua
565 stars 53 forks source link

user type aliases? #34

Closed raould closed 8 years ago

raould commented 9 years ago

hi, I didn't notice it in the paper -- i'd like to be able to do things like (making up the syntax, here, as well as the semantics, hopefully you and your associates would know a good/better way to do the overall thing):

usertypealias pos:number -- one coordinate of an n-dimensional position. usertypealias vel:number -- one coordinate of an n-dimensional velocity. usertypealias acc:number -- one coordinate of an n-dimensional acceleration. interface P { x:pos, y:pos, vx:vel, vy:vel, ax:acc, ay:acc } local P = { x=0:pos, y=0:pos, vx=0:vel, vy=0:vel, ax=0:acc, zy=0:acc } function P:step(dt:number) self.vx = (self.ax:number * dt):vel -- ok due to casting self.vy = self.ay * dt -- type error. self.vy = (self.ay:string) * dt -- type error since ay is acc which is number, not string. local foobar:acc = self.ax * self.ay -- works ok, 'inherits' number's functions/method. local foobar:number = self.ax * self.ay -- type error. end