I'm having fun learning nim by converting Advent of Code solutions I wrote in VBA.
In VBA you can shortcut long qualifiers using the With statement.
Private Sub VmEquals()
With s.Processor
.Memory3 = IIf(.Memory1 = .Memory2, 1, 0)
.ProgramCounter = .ProgramCounter + 4
End With
End Sub
In nim I eventually got to
proc vmEquals(self: Computer) =
var my = self.s.processor
my.memory3 = if my.memory1 == my.memory2: 1 else: 0
my.programCounter = my.programCounter + 4
My question is (having come up blank in searches), is there a more idiomatic way in nim that avoids having to declare an intermediate variable.
$ nimble search with
cascade:
url: https://github.com/citycide/cascade (git)
tags: macro, cascade, operator, dart, with
description: Method & assignment cascades for Nim, inspired by Smalltalk & Dart.
license: MIT
website: https://github.com/citycide/cascade
with:
url: https://github.com/zevv/with (git)
tags: with, macro
description: Simple 'with' macro for Nim
license: MIT
website: https://github.com/zevv/with
Stefan thanks for the prompt response. As ever, sometimes you just miss the key tag for searching. The With package seems to do the trick and hasremoved a huge amount of repetitive noise in object methods. I also tested Cascade but I experienced problems.
I also followed up on inc but like += it didn't work. I'm presuming that this is because 'programCounter' is defined as an object property rather than a field.
Before and after with
before
proc vmJumpIfTrue(self: Computer) =
if self.s.processor.memory1 != 0:
self.s.processor.programCounter = self.s.processor.memory2
else:
self.s.processor.programCounter = self.s.processor.programCounter + 3
after
proc vmJumpIfTrue(self: Computer) =
with self.s.processor:
if memory1 != 0:
programCounter = memory2
else:
programCounter = programCounter + 3
Very, very nice a clean.
I also tested Cascade but I experienced problems.
I'm the author of cascade. If you've run into problems I'd appreciate more info, please open an issue if you're able.