You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is just random notes. Feel free to comment with the correct syntax. The big picture is to type the data in the Record and Tuple with native types.
interfaceIPoint{x:int32,y:int32}constship1:IPoint= #{x: 1,y: 2};// ship2 is an ordinary object:constship2:IPoint={x: -1,y: 3};functionmove(start:IPoint,deltaX:int32,deltaY:int32):IPoint{// we always return a record after movingreturn #{x: start.x+deltaX,y: start.y+deltaY,};}constship1Moved=move(ship1,1,0);// passing an ordinary object to move() still works:constship2Moved=move(ship2,3,-1);console.log(ship1Moved===ship2Moved);// true// ship1 and ship2 have the same coordinates after moving
(I used int32 above because I assume float32 and floating point comparisons aren't really supposed to be used with Record).
constmeasures= #[uint8(42),uint8(12),uint8(67),"measure error: foo happened"];// Accessing indices like you would with arrays!console.log(measures[0]);// 42console.log(measures[3]);// measure error: foo happened// Slice and spread like arrays!constcorrectedMeasures= #[
...measures.slice(0,measures.length-1),int32(-1)];console.log(correctedMeasures[0]);// 42console.log(correctedMeasures[3]);// -1// or use the .with() shorthand for the same result:constcorrectedMeasures2=measures.with(3,-1);console.log(correctedMeasures2[0]);// 42console.log(correctedMeasures2[3]);// -1// Tuples support methods similar to Arraysconsole.log(correctedMeasures2.map(x=>x+1));// #[43, 13, 68, 0]
How do you type variadic Tuples when returning them from a function? I have no idea.
https://github.com/tc39/proposal-record-tuple
This is just random notes. Feel free to comment with the correct syntax. The big picture is to type the data in the Record and Tuple with native types.
(I used int32 above because I assume float32 and floating point comparisons aren't really supposed to be used with Record).
How do you type variadic Tuples when returning them from a function? I have no idea.
Random scribbles:
Record:
Tuple:
The text was updated successfully, but these errors were encountered: