Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Last active August 29, 2015 13:56
Show Gist options
  • Save PhilipWitte/9317301 to your computer and use it in GitHub Desktop.
Save PhilipWitte/9317301 to your computer and use it in GitHub Desktop.

After a recent reddit post and the resulting IRC discussion it caused on Nimrod's syntax, I came up with a couple of changes to the syntax which have a realistic migration path (in theory), and allow for Nimrod to expand to a syntax which could end most of the complaints we here from the C/Java/etc camp, without stepping on the toes of those already happy with the existing syntax.

Instead of a lengthy explanation, i'm sure the code can somewhat speak for itself..

# First, we change set's to @{} instead of {} which..
# ..is consistent with seq and allows us to repurpose {}
var mySet = @{ A, B, C }
var mySeq = @[ a, b, c ]


# Force/use ':' where ever a "body" is consumed which allows both indent style..
# ..and expansion to bracket-style for both types and procs (and even if/else/custom-macros/ etc)

# the follow types are all identical..

type Foo* {.final.} = object: # induces white-space significant 'body'
  a: int
  b: float

type Foo* {.final.} = object [ # induces comma separated 'body' (for passing tuples around mostly)
  a: int,
  b: float
]

type Foo* {.final.} = object { # induces white-space insignificant 'body'
  a: int
  b: float
}


# Move proc pragmas to the left (like types), and swap placement of '=' and  ':'..
# ..which makes it's meaning consistent with type declares (and if/else/etc)

# the following procs are identical...

proc bar*(f:ref Foo) {.noSideEffect.} = float:
  if f.isNil: 0
  else: f.a + f.b

proc bar*(f:ref Foo) {.noSideEffect.} = float {
  if f.isNil: 0
  else: f.a + f.b
}

# Shorthands..
type Foo: bar: int
proc bar: echo ".."

Some additional arguments and reasons I think this is a good idea

  • no one can complain about bracket/indent style anymore. Just like case-style, it's your choice.
  • both proc and type's placement of '=', ':', and {.pargmas.} is consistent. Also ':' use more consistent with if/else/custom-macros/etc
  • both Python and C camp should be happy. This should even appeal to JS devs. Eg, "proc evt = int { 0 }" sorta feels like "var evt = function() { return 0 }"
  • white-space in/significant blocks could be intermixed (the line which starts the significant block is considered 'base whitespace'), and insignificant blocks could be an extension to the parser after '='/':' are flipped for procs and ':' is enforced

Question

':' needs to be enforced so you know where the body begins, right? It's possible even the ':' could be omitted (like existing object syntax) Thoughts? Opinions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment