Guest User

Untitled

a guest
Mar 28th, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.93 KB | None | 0 0
  1. import with
  2.  
  3. type
  4.   Bob = object
  5.     y: int
  6.   Test = object
  7.     x: Bob
  8.  
  9. proc `$`(x: Bob): string =
  10.   "Bob[" & $x.y & "]"
  11.  
  12. # This could be defined by a user, or it could be absent
  13. #proc toHtml(x: Bob): string =
  14. #  $x.y
  15.  
  16. proc toCss(x: Bob): string =
  17.   "(This is CSS: " & $x.y & ")"
  18.  
  19. # This would be automatically generated by autotemplate
  20. proc toHtml(x: Test): string =
  21.   proc tt(x: auto): string =
  22.     when compiles(toHtml(x)):
  23.       toHtml(x)
  24.     else:
  25.       $x
  26.   proc `$`(x: auto): string =
  27.     tt(x)
  28.   with x:
  29.     "Hello: " & $x & " world"
  30.    
  31. proc toCss(x: Test): string =
  32.   proc tt(x: auto): string =
  33.     when compiles(toCss(x)):
  34.       toCss(x)
  35.     else:
  36.       $x
  37.   proc `$`(x: auto): string =
  38.     tt(x)
  39.   with x:
  40.     "Hello: " & $x & " world"
  41.  
  42. echo Test(x: Bob(y: 42)).toHtml # This should output: Hello: Bob[42] world
  43. echo Test(x: Bob(y: 42)).toCss # This should output: Hello: (This is CSS: 42) world
Advertisement
Add Comment
Please, Sign In to add comment