Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. Pseudocode for different ways nice programming by context could work:
  2.  
  3. My initial suggestion was this:
  4. if c < 0: it.le else: it.ri
  5. if context == nil:
  6. context = n
  7. return
  8. it = context
  9.  
  10. Here you have a variable context that is automatically created in the same manner result is for functions.
  11.  
  12. Since then I learned about byAddr (formerly known as byRef), and if I'm correct about it is used it would look like this:
  13.  
  14. byAddr context = if c < 0: it.le else: it.ri
  15. if context == nil:
  16. context = n
  17. return
  18. it = context
  19.  
  20. There are three differences here:
  21. 1) explicit use of byAddr which also has other uses and can be used by other means than if.
  22. 2) the context variable name is programmer defined, not by convention, this makes the code longer,
  23. but also enables nesting of this concept
  24. 3) there's no idented block, which I see as a disadvantage, because it signified where the context is
  25. relevant, for this block of code only and not for what follows, which is the idea of "context"
  26. But this is also a matter of taste, ..., could be good as well
  27.  
  28. There was also an idea by PMunch:
  29.  
  30. template withContext(check, a, b, body): untyped =
  31. if check:
  32. template context: untyped = a
  33. body
  34. else:
  35. template context: untyped = b
  36. body
  37.  
  38. (c < 0).withContext(it.le, it.ri):
  39. if context == nil:
  40. context = n
  41. return
  42. it = context
  43.  
  44. 1) Here he have an indented block.
  45. 2) withContext is descriptive
  46. 3) But is there any chance this could become idiomatic Nim? I used repetitive code from Nim's docs to make the example, because I wanted to make the case that there's currently no idiomatic way to solve this.
  47.  
  48. Another idea for a special syntax I just had:
  49.  
  50. way: # block that defines the way in that something. that depends on a context dependend variable, should be done
  51. if context == nil:
  52. context = n
  53. return
  54. it = context
  55.  
  56. if c < 0: do it.le else: do it.ri # do initiates the way that something should be acted upon either it.le or it.ri here
  57. # either it.le or it.ri became the context
  58.  
  59. 1) this syntax is independend from if or any other structure, could work with case or anything else
  60. 2) do always sets context to the object given. Then executes last defined way block with that context.
  61. 3) new way blocks override old way blocks. There could also be way blocks inside other way blocks without an issue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement