Guest User

Untitled

a guest
Jan 22nd, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. The following snippet was taken from https://nim-lang.org/docs/tut2.html#generics
  2.  
  3. if c < 0:
  4. if it.le == nil:
  5. it.le = n
  6. return
  7. it = it.le
  8. else:
  9. if it.ri == nil:
  10. it.ri = n
  11. return
  12. it = it.ri
  13.  
  14. As you can see the if and the else block have identical logic, with the only difference being
  15. that they operate on different variables.
  16.  
  17. Actually this sort of code where you refactor it by creating a intermediary reference
  18. for the thing you want to do something to, is rather common in programming, which is
  19. why a different notation might be nice:
  20.  
  21. if c < 0: it.le else: it.ri
  22. if context == nil:
  23. context = n
  24. return
  25. it = context
  26.  
  27. Here the if-else is one-lined and two distinguishing factor is automatically
  28. referenced by the compiler generated context identifier. Once the programmer knows how
  29. this works, this should be pretty easy to write/read/maintain.
Advertisement
Add Comment
Please, Sign In to add comment