I try to compile the following code:

type
  TPerson = object of TObject
    name*: string 
    age: int

  TStudent = object of TPerson
    id: int

var
  student: TStudent
  person: TPerson

student = TStudent(name: "Anton", age: 5, id: 2)

but I get the following error:

Error: undeclared field: 'id'

How is id not declared? The code sample is straight from the tutorial. Did something in the language change that is not reflected in the tutorial?


Also, if I try to run the following code:

student = TStudent(name: "Anton")
echo student.name

I get the following error:

SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Why?

有帮助吗?

解决方案

I'm afraid you are likely using the last stable release of Nimrod (version 0.9.2 was released in May 2013) or an old git checkout, what you highlight are bugs in the compiler. Right now with git version https://github.com/Araq/Nimrod/commit/75ca6eb34fbc983d88961664fd1cfce5f8b44abf both your first and last example work without problems. In the past months several bugs have been fixed related to object inheritance in the language (issue 179, issue 520, issue 575 and issue 563).

If you decide to build yourself a git version (it's not scary at all, though sometimes there are regressions in the language) please use the build instructions from the git since the website might not have been updated yet.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top