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
    
    
    Run

It took me some time to realize that newSeq created 2 nulled objects and I 
needed to use 
    
    
    newSeqOfCap
    
    
    Run

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),"*****"
    
    
    Run

It's something I miss from C# where I can write like
    
    
      $"**** Time elapsed {pe.name} for {pe.t1-pe.t0} *****"
    
    
    Run

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()
    
    
    Run

Reply via email to