Can someone point me to which document describes the order of execution of code in files. In particulate why the let statement in the code below executes before any of the procs.
In file Day01.nim
import strutils
import strformat
import sequtils
import Constants
const InputData = "Day01.txt"
type
State = object
Integers: seq[int]
let s =State( Integers:toSeq((RawDataPath & InputData).lines).map(parseInt) )
proc Part01() =
var myResult:int=0
for myIndex in 1..<s.Integers.len():
<code deleted>
proc Part02() =
var myResult:int=0
var mySum:seq[int]
for myindex in 2..<s.Integers.len():
<code deleted>
proc Execute*()=
Part01()
Part02()
In file Main
import Day01
Day01.Execute()
@jyapayne Mainly VBA and a little bit of C#. I'm not a professional programmer. I'm not used to code that is not enclosed in a Method (procedure definition) inside a names module or class . In VBA the let statement above would be illegal. I wanted to read up on it to be aware of what I can and can't do with such 'unwrapped code'
I'm still waiting for a pointer to a document/reference.
@freeflow, please don't spam your own topic with multiple comments. And the snarkier you are the less likely people are to help you.
The order of execution of a Nim program is simply so straight forward that I'm not sure we have any reference material/documentation for it. The closest thing would probably be our beginner guides.
One way to think about it is that everything you put in the global scope (ie. not inside any procedure) will be concatenated together into a main procedure. So in your case you might see your program (fully combined) as:
import strutils
import strformat
import sequtils
import Constants
const InputData = "Day01.txt"
type
State = object
Integers: seq[int]
proc Part01() =
var myResult:int=0
for myIndex in 1..<s.Integers.len():
<code deleted>
proc Part02() =
var myResult:int=0
var mySum:seq[int]
for myindex in 2..<s.Integers.len():
<code deleted>
proc Execute*()=
Part01()
Part02()
proc Main() =
let s = State(Integers: toSeq((RawDataPath & InputData).lines).map(parseInt))
Day01.Execute()
Main()
This of course isn't valid Nim code since the variable s now exists only in the Main procedure scope. But if you think about it in an object/method sense then s would be a member of an invisible Program class, the Main procedure would be run for this class, populate the s variable, and then call the rest of your procedures.
@PMunch. Apologies, I was trying to attach my reply to the comment by @Argl but messed up so instead they got added as general comments. Whilst I could edit to remove the text, I wasn't allowed to delete the (now empty) comments.
Thanks for the explanation. The analagous situatione in VBA would be code the Class_Initialize method. Such code is run the first time an instance of the class is encountered on the RHS of an expression.