-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
sameType
magic doesn't work for seq (and generics?)
#14021
Comments
IMO, it should be done this way. macro testSeq2(x: typed): untyped =
let seqIntType = getType(seq[int])[1]
let repr = repr seqIntType
let res = x.sameType(seqIntType)
result = quote do:
echo "x is ", `repr`, ": ", bool(`res`), " (Nim VM `sameType`)"
testSeq2(x)
|
* index_select should use SomeInteger not SOmeNumber * Overload index_select for arrays and sequences * Masked Selector overload for openarrays * Add masked overload for regular arrays and sequences * Initial support of Numpy fancy indexing: index select * Fix broadcast operators from #429 using deprecated syntax * Stash dispatcher, working with types in macros is a minefield nim-lang/Nim#14021 * Masked indexing: closes #400, workaround nim-lang/Nim#14021 * Test for full masked fancy indexing * Add index_fill * Tensor mutation via fancy indexing * Add tests for index mutation via fancy indexing * Fancy indexing: supports broadcasting a value to a masked assignation * Detect wrong mask or tensor axis length * masked axis assign value test * Add masked assign of broadcastable tensor * Tag for changelog [skip ci]
Unfortunately it does not, it works with type Tensor[T] = object
data: T
macro testTensorInt(x: typed): untyped =
let tensorIntType = getType(Tensor[int])[1]
let repr = repr tensorIntType
let res = x.sameType(tensorIntType)
result = quote do:
echo "x is ", `repr`, ": ", bool(`res`), " (Nim VM `sameType`)"
var x2: Tensor[int]
testTensorInt(x2) |
Slightly improved version works import macros
type Tensor[T] = object
data: T
macro testTensorInt(x: typed): untyped =
let tensorIntType = getTypeInst(Tensor[int])[1]
let repr = repr tensorIntType
let res = x.sameType(tensorIntType)
result = quote do:
echo "x is ", `repr`, ": ", bool(`res`), " (Nim VM `sameType`)"
var x2: Tensor[int]
testTensorInt(x2) |
And this one doesn't work (the type Tensor[T] = object
data: T
macro testTensor(x: typed): untyped =
let tensorType = getTypeInst(Tensor)
let repr = repr tensorType
let res = x.sameType(tensorType)
result = quote do:
echo "x is ", `repr`, ": ", bool(`res`), " (Nim VM `sameType`)"
var x2: Tensor[int]
testTensor(x2) |
I'm confused, the |
Typeof can extract type from anything that has type including NimNode if it is typed NimNode |
All you have done there is ask the question : NimNode is NimNode? |
the bug is that instead, sameType API should be fixed to give CT error for nodes that don't have a type, like the one you built: |
You can't build types like that:
|
If you want to compare to concrete types then |
Ideally an equivalent to |
FWIW I don't like The compiler should simply embrace its strong typing and expose PNode as NimNode (as it does) and PType as NimType (as it doesn't do yet). I have a branch that explores that. Since proc `[]`(a: NimType, index: int): NimType
proc kind(a: NimType): NimTypeKind # more type-safe than NimNode.typeKind
proc isType(a, b: NimType): bool # represents is
template sameType(a, b: NimType): bool = a.isType(b) and b.isType(a)
proc getType(a: NimNode): NimType Benefits:
I believe this will help with your RFC To your specific point regarding having D20200420T030114 |
* Introduce Fr type: finite field over curve order. Need workaround for nim-lang/Nim#16774 * Split curve properties into core and derived * Attach field properties to an instantiated field instead of the curve enum * Workaround nim-lang/Nim#14021, yet another "working with types in macros" is difficult nim-lang/RFCs#44 * Implement finite field over prime order of a curve subgroup * skip OpenSSL tests on windows
The
sameType
magic is supposed to allow type comparison between NimNode.I expect that the use is similar to the
is
operator but in the VM.It works for simple type like int but does not for
seq[int]
, exampleOutput:
A very cumbersome workaround would be to delegate the comparison to "normal Nim" via
when x is seq[int]
and have that code call a delegate macro, if we have some context to pass to it we can pass it via a {.compileTime.} variable that stores NimNode.This is another instance of "Working with types in macros is difficult" nim-lang/RFCs#44
The text was updated successfully, but these errors were encountered: