Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pseudocode for different ways nice programming by context could work:
- My initial suggestion was this:
- if c < 0: it.le else: it.ri
- if context == nil:
- context = n
- return
- it = context
- Here you have a variable context that is automatically created in the same manner result is for functions.
- Since then I learned about byAddr (formerly known as byRef), and if I'm correct about it is used it would look like this:
- byAddr context = if c < 0: it.le else: it.ri
- if context == nil:
- context = n
- return
- it = context
- There are three differences here:
- 1) explicit use of byAddr which also has other uses and can be used by other means than if.
- 2) the context variable name is programmer defined, not by convention, this makes the code longer,
- but also enables nesting of this concept
- 3) there's no idented block, which I see as a disadvantage, because it signified where the context is
- relevant, for this block of code only and not for what follows, which is the idea of "context"
- But this is also a matter of taste, ..., could be good as well
- There was also an idea by PMunch:
- template withContext(check, a, b, body): untyped =
- if check:
- template context: untyped = a
- body
- else:
- template context: untyped = b
- body
- (c < 0).withContext(it.le, it.ri):
- if context == nil:
- context = n
- return
- it = context
- 1) Here he have an indented block.
- 2) withContext is descriptive
- 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.
Advertisement
Add Comment
Please, Sign In to add comment