Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The following snippet was taken from https://nim-lang.org/docs/tut2.html#generics
- if c < 0:
- if it.le == nil:
- it.le = n
- return
- it = it.le
- else:
- if it.ri == nil:
- it.ri = n
- return
- it = it.ri
- As you can see the if and the else block have identical logic, with the only difference being
- that they operate on different variables.
- Actually this sort of code where you refactor it by creating a intermediary reference
- for the thing you want to do something to, is rather common in programming, which is
- why a different notation might be nice:
- if c < 0: it.le else: it.ri
- if context == nil:
- context = n
- return
- it = context
- Here the if-else is one-lined and two distinguishing factor is automatically
- referenced by the compiler generated context identifier. Once the programmer knows how
- this works, this should be pretty easy to write/read/maintain.
Advertisement
Add Comment
Please, Sign In to add comment