First, gratz nim team on releasing v.1.0 I'm just sharing my...first impressions :) I'm a C# game developer that wants to learn something new and fresh, it's difficult to change mindset sometimes though.
I decided to write something very stupid like time profiler "hello world" and I thought it will be EASY PIZY but...I ended up getting SIGSEGV: Illegal storage access. (Attempt to read from nil?)
I like to try to solve my problems myself and I realized that it must be something stupid about my code
var
elements = newSeq[ProfileElement](2)
index: int
It took me some time to realize that newSeq created 2 nulled objects and I needed to use
newSeqOfCap
It's something very unfamiliar for me and I still didn't decide whether I like it or not. The thing that made me sad was string formatting. A lot of useless comma stuff in the string
echo "**** Time elapsed ",$(pe.name)," for ", $(pe.t1-pe.t0),"*****"
It's something I miss from C# where I can write like
$"**** Time elapsed {pe.name} for {pe.t1-pe.t0} *****"
But maybe I don't know something about formatting in Nim. In the end, the code looks something like this XD
I think I'll hang around and try to write something more interesting and complex to decide if Nim is great for game dev :)
type
ProfileElement = object
name: string
t0, t1: float
var
elements = newSeqOfCap[ProfileElement](2)
index: int
proc newProfileElement*(arg: string): ProfileElement =
result = ProfileElement(
name: arg,
t0: cpuTime()
)
proc Start*(arg:string)=
var el = newProfileElement(arg)
el.name = arg
el.t0 = cpuTime()
elements.add(el)
proc End*()=
var el = elements[index]
el.t1 = cpuTime()
index += 1
proc Print*()=
for pe in elements:
echo "**** Time elapsed ",$(pe.name)," for ", $(pe.t1-pe.t0),"*****"
index = 0
elements.setLen(0)
profile.Start("Test1")
profile.End()
profile.Start("Test2")
profile.End()
profile.Start("Test3")
profile.End()
profile.Print()
What you are missing is strformat:
import strformat
proc Print*()=
for pe in elements:
echo &"**** Time elapsed {pe.name} for {pe.t1-pe.t0} *****"
index = 0
elements.setLen(0)
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)
I get a mistake here:
proc release(entity: ref ent)=
add(entStash, entity)
entity.id = 0
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];
And if I want to pass something back I understand that the stuff will be copied.
args[10] = arg;
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
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);
}
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)
1.
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.
new has a couple of overload for ref types. You can use it but you might get ambiguous proc warnings if your type happens to be a ref type. Also the convention is: initMyType if your type has value semantics and newMyType if your type has reference semantics. (newSeq was a mistake from Nim young days).
proc release(entity: ref ent)=
add(entStash, entity)
if not entity.isNil:
entity.id = 0
else:
new entity
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 :)
Unsigned overflow is defined as wrap around, while signed overflow triggers an exception. If you need the performance and you're sure that won't happen you can turn this off, like also array/seq bound checking, etc.