Next questions ; )
Is it OK to name procs like new ? As far as I understood it's a "system" word
but compiling didn't give me any errors.
Also without ref in [ent]
entStash = newSeqOfCap[ent](256)
Run
I get a mistake here:
proc release(entity: ref ent)=
add(entStash, entity)
entity.id = 0
Run
It's more like I think in scope of C# but there I don't have to bother about
how to define array of values. I know that structs are value type that are
copied so If I want to get a struct by ref I do something like
ref var arg = ref args[0];
Run
And if I want to pass something back I understand that the stuff will be
copied.
args[10] = arg;
Run
So do I have to always write a copy of my obj to pass it to a "non ref" array
in Nim explicitly or there is another way that I don't see yet ?
proc release(entity: ref ent)=
var e : ent
e.id = entity.id
e.age = entity.age;
add(entStash, e)
entity.id = 0
Run
Also, I thought I will get overflow exception with uint8 going with values more
that 255 but it's automatically went back to zero. In c# I have to use
something like to achieve that. Is there any black magic about overflows in Nim
I need to know ? Thanks in advance :)
unchecked
{
age = (byte) (pop.age + 1);
}
Run
type
ent* = object
id*:int
age*: uint8
var
lastID = 1
entStash = newSeqOfCap[ent](256)
proc new() : ref ent =
new(result)
if entStash.len > 0:
result.id = entStash[0].id
result.age = entStash[0].age+1
entStash.del(0)
else:
let id = lastID
lastID += 1
result.id = id
result.age = 0
proc release(entity: ref ent)=
var e : ent
e.id = entity.id
e.age = entity.age;
add(entStash, e)
entity.id = 0
for i in 0..280:
var e = entity.new()
echo e.age
entity.release(e)
Run