Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. // embellishment on comment in issue #1595
  2.  
  3. enum NumberGroupType {
  4. // Measure types:
  5. // (* and /) yield compound types by adding or subtracting exponent arrays of the operands
  6. // (+ and -) restricted to work only on operands with same exponent array
  7. // comparators (==, <=, ..) restricted to work only on operands with same exponent array
  8. // e.g OK: 1[km] + 4[km] or 2[km] == 2.1[km] .. Not OK: 2[km] == 2[s].
  9. Measure_f64,
  10. Measure_i32,
  11.  
  12. // Bitflag types:
  13. // No operators allowed except equality checks and bitwise AND, OR, and NOT
  14. Bitflag_u32,
  15.  
  16. // only equality check allowed
  17. Key_u32,
  18.  
  19. // all operators allowed, exponent array ignored, but cannot be mixed
  20. // with numbers from another NumberGroup or primitives
  21. Normal,
  22.  
  23.  
  24. }
  25.  
  26. // used as an interface
  27. struct NumberGroup{
  28. // used by compiler to check compatability between operands
  29. getExponentArray : fn(self: @This()) []i8,
  30.  
  31. // used by compiler to create correct types after operations
  32. fromExponentArray : fn([]i8) : @This(),
  33.  
  34. // used by compiler to create a "named type" from an exponent array when possible
  35. // also to used to check 2nd parameter to "@distinct(number,NumberGroupInstance)" call
  36. namedTypes : []@This(),
  37.  
  38. // used by compiler to select which rules to apply regarding operand compatability,
  39. // which subset of operators are available, and which primitive number type to accept
  40. numberType : NumberGroupType,
  41. }
  42.  
  43. // assume <...> means "implements ..." in some form
  44. const PhysicalUnit = struct<NumberGroup>{
  45.  
  46. const numberType: NumberType = NumberType.Measure_f64;
  47.  
  48. e10: i8 = 0, //unit prefix, e.g kilo when e10=3, nano when e10=-9
  49. e60: i8 = 0 // unit prefix for time, e.g minute when e60=1, hour when e60=2
  50. m: i8 = 0, // meter
  51. s: i8 = 0, // second
  52. kg: i8 = 0, // kilogram
  53. k: i8 = 0, // kelvin
  54. mi: i8 = 0, // miles, imperial unit
  55. mo: i8 = 0, // mole
  56. A: i8 = 0, // ampere
  57. cd : i8 = 0, // candela
  58.  
  59. const getExponentArray = fn(self:PhysicalUnit) [_]i8{
  60. return []i8{e10,e60,m,s,kg,k,mi,mo,A,cd};
  61. }
  62.  
  63. const fromExponentArray = fn(arr : []i8) PhysicalUnit{
  64. return PhysicalUnit{
  65. .e10 = arr[0],
  66. .e60 = arr[1],
  67. .m = arr[2],
  68. // ...
  69. .cd = arr[9],
  70. }
  71. }
  72.  
  73. const namedtypes : [_]PhysicalUnit = [_]{
  74. phys_UnitLess = PhysicalUnit{};
  75. phys_Meter = PhysicalUnit{.m=1};
  76. phys_Second = PhysicalUnit{.s=1};
  77. phys_PrSecond = PhysicalUnit{.s=-1};
  78. phys_Newton = PhysicalUnit{.s=-2,kg=1,m=1}
  79. // ..
  80. phys_SiMomentum = PhysicalUnit{.m=1,s=-1,kg=1};
  81. phys_KmPrHr = PhysicalUnit{.e10=3, .m=1, .e60=-2, .s=-1}
  82. }
  83.  
  84.  
  85.  
  86. }
  87. }
  88.  
  89.  
  90.  
  91.  
  92. // Usage example:
  93. // * const x = @distinct( NUMBER , NUMBERGROUP_IMPL_INSTANCE);
  94. // * Where:
  95. // * - ´NUMBER´ is any builtin number type: u8, f64, i32,
  96. // * - ´NUMBERGROUP_IMPL_INSTANCE´ is an instance of a struct that implements (or embeds) a built in
  97. // * NumberGroup interface that the compiler recognizes and can use for compile time checks.
  98.  
  99.  
  100. // "phys_..." variables are instances of the struct PhysicalUnit
  101. const x = @distinct(0.13,phys_Second);
  102. const y = @distinct(20.3,phys_Meter);
  103. const z = y/z; // same as @distinct(20.3/0.13,phys_MeterPrSecond);
  104.  
  105. // the two below expressions become compile errors as they are not allowed by NumberType.Measure_f64
  106. _ = z >> 2;
  107. _ = z & 5;
  108.  
  109. // usage in function. assume workaround for treating instance ´phys_Newton' as a type
  110. fn calculateAcceleration(ball: Ball, force: phys_Newton) phys_MeterPrSecond2{
  111. // ... use fields like ball.mass : phys_Kilogram, etc in calculations
  112. //
  113. }
  114.  
  115.  
  116. // "db_..." variables are instances of the struct MyDbKeys, not shown
  117. const x = @distinct(4,db_Person);
  118. const y = @distinct(2,db_Task);
  119. // the 5 below expressions become compile errors as they are not allowed by NumberType.Key_u32
  120. _ = x + y;
  121. _ = x / y;
  122. _ = x + x
  123. _ = x == y;
  124. _ = x > y;
  125.  
  126. // the below expressions are allowed
  127. const x2 = @distinct(5,db_Person);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement