Skip to content

Instantly share code, notes, and snippets.

@vito
Created March 4, 2011 19:25
Show Gist options
  • Save vito/855531 to your computer and use it in GitHub Desktop.
Save vito/855531 to your computer and use it in GitHub Desktop.
quanto method style guidelines
if you have to write a unary method to accompany a keyword method
(e.g. for sending no args), just define the unary method
if you're going to use multiple blocks, use keyword
if a block value is likely to be used, consider using unary postfix or
keyword (not a huge deal though, given that & is pretty easy)
unary prefix can be better for DSLs, as it immediately shows the purpose
of the block rather than tacking it on the end
if the arguments for the method are directly related to the block (ie
passed as args), consider prefix.
{ puts("hi") } spawn: ()
{ a | p(a) } spawn: 1
{ a b | p(a + b) } spawn: (1, 2)
pros:
- defined on Block, not cluttering global namespace
- just acts on the value rather than special syntax
cons:
- a little clumsy
- have to send () for no args
- or define #spawn, but then you might as well just have that one method
- have to remember to send "spawn" after you've written the body
- block's arguments are on the opposite side of the expr,
far away from what you're passing
{ puts("hi") } spawn
{ a | p(a) } spawn(1)
{ a b | p(a + b) } spawn(1, 2)
pros:
- succinct
- defined on Block, not cluttering global namespace
- just acts on the value rather than special syntax
cons:
- have to remember to send "spawn" after you've written the body
- block's arguments are on the opposite side of the expr,
far away from what you're passing
spawn { puts("hi") }
spawn(1) { a | p(a) }
spawn(1, 2) { a b | p(a + b) }
pros:
- succinct
- immediately says what's happening to the next block
- values passed are right next to block args
cons:
- defined at the top-level
- have to use & to send a block value:
spawn(1, 2, &foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment