Advertisement
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.
- Another idea for a special syntax I just had:
- way: # block that defines the way in that something. that depends on a context dependend variable, should be done
- if context == nil:
- context = n
- return
- it = context
- 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
- # either it.le or it.ri became the context
- 1) this syntax is independend from if or any other structure, could work with case or anything else
- 2) do always sets context to the object given. Then executes last defined way block with that context.
- 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