Nimcomer

A newcomers collection of Nim information and code snippets.

I tried looping over a sequence of objects:

  for rule in self.rules:
    if rule.evaluate():
      var event: Event = rule.fire()
      ## other code

But this did not work well with the fire proc which changes the state of the rule object. The corresponding error looks like this:

ui.nim(281, 30) Error: type mismatch: got < Rule > but expected one of: proc fire(self: var Rule): Event for a 'var' type a variable needs to be passed, but 'rule' is immutable

expression: fire(rule)

However, looping over the sequence with the mitems iterator makes state change possible.

Here is the fixed code:

  for rule in self.rules.mitems():
    if rule.evaluate():
      var event: Event = rule.fire()
      ## other code

I will post any new code snippet as I stumble over it.

#Nim #Nimlang #newcomer #noob #coding #error #beginner