Deprecate baseType (#148)

* Switch to compile time error for ARC / ORC

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
Tanguy 2022-11-10 08:35:52 +01:00 committed by GitHub
parent ba687c37a3
commit 989047dd76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 19 deletions

View File

@ -59,14 +59,18 @@ when not compiles(len((1, 2))):
# Get an object's base type, as a cstring. Ref objects will have an ":ObjectType"
# suffix.
# From: https://gist.github.com/stefantalpalaru/82dc71bb547d6f9178b916e3ed5b527d
proc baseType*(obj: RootObj): cstring =
when not defined(nimTypeNames):
raiseAssert("you need to compile this with '-d:nimTypeNames'")
else:
when not defined(nimTypeNames):
proc baseType*(obj: RootObj): cstring {.error: "baseType requires -d:nimTypeNames".}
proc baseType*(obj: ref RootObj): cstring {.error: "baseType requires -d:nimTypeNames".}
elif defined(gcArc) or defined(gcOrc):
proc baseType*(obj: RootObj): cstring {.error: "baseType is not available in ARC/ORC".}
proc baseType*(obj: ref RootObj): cstring {.error: "baseType is not available in ARC/ORC".}
else:
proc baseType*(obj: RootObj): cstring {.deprecated.} =
{.emit: "result = `obj`->m_type->name;".}
proc baseType*(obj: ref RootObj): cstring =
obj[].baseType
proc baseType*(obj: ref RootObj): cstring {.deprecated.} =
obj[].baseType
macro enumRangeInt64*(a: type[enum]): untyped =
## This macro returns an array with all the ordinal values of an enum

View File

@ -9,21 +9,24 @@ when defined(nimHasUsed):
suite "Objects":
test "baseType":
type
Foo = ref object of RootObj
Bar = ref object of Foo
Baz = object of RootObj
Bob = object of Baz
Bill = ref object of Bob
when (not defined(nimTypeNames)) or defined(gcOrc) or defined(gcArc):
skip()
return
else:
type
Foo = ref object of RootObj
Bar = ref object of Foo
Baz = object of RootObj
Bob = object of Baz
Bill = ref object of Bob
var
foo = Foo()
bar = Bar()
baz = Baz()
bob = Bob()
bill = Bill()
var
foo = Foo()
bar = Bar()
baz = Baz()
bob = Bob()
bill = Bill()
when defined(nimTypeNames):
check:
foo.baseType == "Foo:ObjectType"
bar.baseType == "Bar:ObjectType"