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

Inferring nested static[T] #3153

Closed
andreaferretti opened this issue Jul 27, 2015 · 2 comments
Closed

Inferring nested static[T] #3153

andreaferretti opened this issue Jul 27, 2015 · 2 comments

Comments

@andreaferretti
Copy link
Collaborator

This does not work

proc outSize[M: static[int], A](xs: array[M, A]): int = M

echo outSize([1, 2, 3, 4])

failing with Error: cannot generate code for: M.

In itself, this is not an issue: I think it has to do with array accepting a range as its first parameter, and it is enough to wrap it into a type with another name:

type Arr[N: static[int], A] = array[N, A]

proc outSize[M: static[int], A](xs: Arr[M, A]): int = M

echo outSize([1, 2, 3, 4]) # 4

It works for nested arrays as well:

echo outSize([
  [1, 2, 3],
  [4, 5, 6]
]) # 2

Unfortunately, trying to get the inner dimension fails:

proc inSize[M, N: static[int]](xs: Arr[M, Arr[N, int]]): int = N

echo inSize([
  [1, 2, 3],
  [4, 5, 6]
])

This fails with Error: cannot instantiate: 'N'. Unlike the previous example, I was not able to find a workaround for this

@yglukhov
Copy link
Member

Latest nim devel now just crashes:

nim.nim(104)             nim
nim.nim(68)              handleCmdLine
main.nim(251)            mainCommand
main.nim(63)             commandCompileToC
modules.nim(214)         compileProject
modules.nim(162)         compileModule
passes.nim(203)          processModule
passes.nim(137)          processTopLevelStmt
sem.nim(458)             myProcess
sem.nim(432)             semStmtAndGenerateGenerics
semstmts.nim(1465)       semStmt
semexprs.nim(910)        semExprNoType
semexprs.nim(2172)       semExpr
semexprs.nim(1759)       semMagic
semexprs.nim(893)        semDirectOp
semexprs.nim(763)        semOverloadedCallAnalyseEffects
semcall.nim(342)         semOverloadedCall
semcall.nim(159)         resolveOverloads
semcall.nim(70)          pickBestCandidate
sigmatch.nim(1695)       matches
sigmatch.nim(1656)       matchesAux
sigmatch.nim(1509)       prepareOperand
semexprs.nim(26)         semOperand
semexprs.nim(2171)       semExpr
semexprs.nim(893)        semDirectOp
semexprs.nim(763)        semOverloadedCallAnalyseEffects
semcall.nim(342)         semOverloadedCall
semcall.nim(159)         resolveOverloads
semcall.nim(70)          pickBestCandidate
sigmatch.nim(1695)       matches
sigmatch.nim(1657)       matchesAux
sigmatch.nim(1430)       paramTypesMatch
sigmatch.nim(1300)       paramTypesMatchAux
sigmatch.nim(758)        typeRel
sigmatch.nim(593)        tryResolvingStaticExpr
semtypinst.nim(490)      replaceTypesInBody
semtypinst.nim(201)      replaceTypeVarsN
semexprs.nim(2255)       semExpr
semexprs.nim(746)        semStaticExpr
vm.nim(1481)             evalStaticExpr
system.nim(333)          evalConstExprAux
vmgen.nim(1767)          genExpr
vmgen.nim(1642)          gen
vmgen.nim(726)           genMagic
vmgen.nim(254)           genx
vmgen.nim(1632)          gen
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

@andreaferretti
Copy link
Collaborator Author

Just wanted to mention that the following workaround works (and it is what I am using in linalg to infer matrix sizes)

type DoubleArray32[M, N: static[int]] = array[M, array[N, float32]]
type DoubleArray64[M, N: static[int]] = array[M, array[N, float64]]

zah added a commit that referenced this issue Jun 10, 2017
zah added a commit that referenced this issue Jun 19, 2017
@Araq Araq closed this as completed in 6f93559 Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants