tgenericconverter.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. output: '''666
  3. 666'''
  4. """
  5. # test the new generic converters:
  6. type
  7. TFoo2[T] = object
  8. x: T
  9. TFoo[T] = object
  10. data: array[0..100, T]
  11. converter toFoo[T](a: TFoo2[T]): TFoo[T] =
  12. result.data[0] = a.x
  13. proc p(a: TFoo[int]) =
  14. echo a.data[0]
  15. proc q[T](a: TFoo[T]) =
  16. echo a.data[0]
  17. var
  18. aa: TFoo2[int]
  19. aa.x = 666
  20. p aa
  21. q aa
  22. #-------------------------------------------------------------
  23. # issue #16651
  24. type
  25. PointTup = tuple
  26. x: float32
  27. y: float32
  28. converter tupleToPoint[T1, T2: SomeFloat](self: tuple[x: T1, y: T2]): PointTup =
  29. result = (self.x.float32, self.y.float32)
  30. proc tupleToPointX(self: tuple[x: SomeFloat, y: SomeFloat]): PointTup =
  31. result = (self.x.float32, self.y.float32)
  32. proc tupleToPointX2(self: tuple[x: SomeFloat, y: distinct SomeFloat]): PointTup =
  33. result = (self.x.float32, self.y.float32)
  34. var t1: PointTup = tupleToPointX((1.0, 0.0))
  35. var t2: PointTup = tupleToPointX2((1.0, 0.0))
  36. var t3: PointTup = tupleToPointX2((1.0'f32, 0.0))
  37. var t4: PointTup = tupleToPointX2((1.0, 0.0'f32))
  38. var x2: PointTup = (1.0, 0.0)