Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected behavior of the "of" operator #178

Closed
AdrianV opened this issue Jul 26, 2012 · 3 comments
Closed

unexpected behavior of the "of" operator #178

AdrianV opened this issue Jul 26, 2012 · 3 comments

Comments

@AdrianV
Copy link
Contributor

AdrianV commented Jul 26, 2012

I think this should compile, but it does not:

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

  TStudent = object of TPerson
    id: int                   

var
  student: TStudent
  person: TPerson


echo student of TStudent # is true
echo student of TPerson # does not compile, should be true, throws Error: 'TStudent' can never be of this subtype
@AdrianV
Copy link
Contributor Author

AdrianV commented Jul 26, 2012

this code refuses to compile, but should IMHO - aborts with:
Error: type mismatch: got (PKeysIterator[string, int], PBtree[string, int])
but expected one of:
initIterator(it: PTreeIterator[T, D], n: PBtree[T, D])
since PKeysIterator[string, int] is a PTreeIterator[string, int] I can't see anything wrong here

I guess the problem is related to the "of" problem , therefore I don't open a new issue:

type
  PBtree* [T,D] = ref TBTree[T,D]
  TItem* [T,D] = object
        key* : T
        value* : D
        node: PBtree[T,D]
        when not (D is string):
          val_set: Bool

  TItems[T,D] = seq[ref TItem[T,D]]
  TBTree* [T,D] = object
        slots: TItems[T,D]
        left: PBtree[T,D]
        count: Int32

  RPath[T,D] = tuple[
    Xi: Int,
    Nd: PBtree[T,D] ]

  PTreeIterator* [T,D] = ref TTreeIterator[T,D]
  TTreeIterator* [T,D] = object 
    path: array[0..30, RPath[T,D]]
    level: int
    current: ref TItem[T,D]

  PKeysIterator* [T,D] = ref TKeysIterator [T,D]  
  TKeysIterator* [T,D] = object of TTreeIterator[T,D]

proc initIterator [T,D] (it: PTreeIterator[T,D], n: PBtree[T,D]) {.inline.} = 
  it.Path[0].Nd = n
  it.Path[0].Xi = -1

proc keys* [T,D](n: PBtree[T,D]): PKeysIterator[T,D] = 
  new(result)
  initIterator(result, n)

var 
  root: PBTree[string, int]
  iter = keys(root)

@Araq
Copy link
Member

Araq commented Jul 26, 2012

Sigh ... See bug #88 ...

@AdrianV
Copy link
Contributor Author

AdrianV commented Jul 26, 2012

sorry for double posting - I haven't seen the other issue. Currently a cast helps in the second example.

@Araq Araq closed this as completed in e54623e Jul 28, 2012
reactormonk pushed a commit to reactormonk/nim that referenced this issue Apr 7, 2014
Clyybber pushed a commit to Clyybber/Nim that referenced this issue Feb 9, 2022
178: minor refactor of unittest implementation r=saem a=krux02

generally speaking unittesting is very important, but the unittest
modules causes nothing but problems. The problem is, unittest pretty
much uses all problematic metaprogramming APIs at the same time. This
should be changed, step by step.

 * `template print(a,b: untyped)` -> `proc print[T](name: string, value: T)`
 * `newIdentNode(":c" & $counter)` -> `genSym`
 * `template assign` -> `newVarStmt`



Co-authored-by: Arne Döring <a.doering@digiblue.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants