I am new to nim. When I compiled the following code, it was successful. But I got some errors, when I run the compiled file.
code is like:
type
Animal = ref object of RootObj
age: int
name: string
Cat = ref object of Animal
playfulness: float
func initCat1(age = 2): Cat =
Cat(age: age, name: "Tom")
proc initCat2(age = 2): Cat =
result.age = age
result.name = "Tom"
let cat1 = initCat1()
let cat2 = initCat2() # run with error: SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error message is like:
Traceback (most recent call last)
D:\code\nim-projects\test-nim\test.nim(18) test
D:\code\nim-projects\test-nim\test.nim(14) initCat2
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
My OS is Windows 11, and my version of nim is "1.6.8".
Any help is appreciated.
Well as you noticed yourself,
Cat(age: age, name: "Tom")
is not the same as
result.age = age: result.name = "Tom"
Maybe the allocation itself for the heap object is missing?
proc initCat2(age = 2): Cat =
new result
result.age = age
result.name = "Tom"
Yeah, that's it.
I just copied the example from nim for python programmers (https://github.com/nim-lang/Nim/wiki/Nim-for-Python-Programmers#self__init__). I have no idea if it worked before, but failed recently? Is it a bug?
original code following:
type
Animal = ref object of RootObj
age: int
name: string
Cat = ref object of Animal
playfulness: float
func initCat(age = 2): Cat =
result.age = age
result.name = "adopt_me"
func increase_age(self: Cat) =
self.age.inc()
proc main() =
var kitten = Cat(name: "Tom")
kitten.increase_age()
assert kitten.name == "Tom"
assert kitten.age == 1
when isMainModule:
main()
runnableExamples:
echo "Optionally some documentation code examples here"
assert 42 == 42
I noticed that it didn't call initCat proc, but if I called the proc it also raised error.
it might be indeed a bit complicated to convert to nimib but one can try (we will likely discover features are missing and some stuff it will be hard to reproduce). it would be fun anyway to do similar projects in nim with nimib (e.g. scraping rosetta code and running with nimib or creating a nim version for the algorithms)
btw shouldn't I see some button to edit the wiki or this is restricted in some way?
If the conversion is just putting the code under nbCode sections and text under nbText sections in nimib, that's ok, it can be done semi-automatically.
If we also want to keep the html style of the wikis, that's more complex. Nimib interface uses Markdown (and do not enable to use html directly if I am not mistaken), so many things might not render as well with Nimib than with the wikis (we can still modify the rendered html but we would miss the point of Nimib).
We might discuss this in another forum post.
do not enable to use html directly if I am not mistaken
you can with nbRawHtml since 3.0 (actually 3.1, in 3.0 was named nbRawOutput)
wiki I think also uses markdown but powered up by Github and nimib's markdown engine (nim-markdown) is likely not supporting them all.
We might discuss this in another forum post.
yep it would be probably better