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

Compilation error when using type-generic proc in type definition #20514

Closed
rotu opened this issue Oct 7, 2022 · 5 comments · Fixed by #22029
Closed

Compilation error when using type-generic proc in type definition #20514

rotu opened this issue Oct 7, 2022 · 5 comments · Fixed by #22029

Comments

@rotu
Copy link
Contributor

rotu commented Oct 7, 2022

What happened?

Using prod in a type definition causes the compiler to fail with an uninformative error message.

import std/math

type Foo[S:static[array[2, int]]] = object
  values: array[prod(S), float]

echo sizeof(Foo[[4,5]])

Curiously, replacing prod with sum causes a different outcome. The below program erroneously prints 1:

import std/math

type Foo[S:static[array[2, int]]] = object
  values: array[sum(S), float]

echo sizeof(Foo[[4,5]])

Nim Version

Nim Compiler Version 1.7.3 [Windows: amd64]
Compiled at 2022-10-07
Copyright (c) 2006-2022 by Andreas Rumpf

active boot switches: -d:release

Current Standard Output Logs

C:\Users\dan\.choosenim\toolchains\nim-#devel\lib\pure\math.nim(357, 3) Error: cannot generate VM code for result

Expected Standard Output Logs

No response

Possible Solution

No response

Additional Information

No response

@rotu
Copy link
Contributor Author

rotu commented Oct 7, 2022

possibly addressed by #20423?

@beef331
Copy link
Collaborator

beef331 commented Oct 8, 2022

It's not ideal but a common workaround is to use a template to intercept the typed code so it can actually compile as such:

import std/math

template arr(a: typed, b: typedesc): auto = array[a, b]

type Foo[S:static[array[2, int]]] = object
  values: arr(prod(S), float)

echo sizeof(Foo[[4,5]])

@rotu
Copy link
Contributor Author

rotu commented Oct 8, 2022

@beef331 Thanks! Any insight as to why the compiler chokes in the first place?

I wound up wrapping prod instead, which seems to also work:

proc intProd(s:seq[int]):int = prod[int](s)

type Foo[S:static[array[2, int]]] = object
  values: array[intProd(S), float]

@bung87
Copy link
Collaborator

bung87 commented Oct 9, 2022

possibly addressed by #20423?

this one may not related to my PR, that one main problem is inner generic doesn't get static T as outter gets, it only get T

@metagn
Copy link
Collaborator

metagn commented Jun 6, 2023

Related #19916 (generic object, static parameter, array index)

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

Successfully merging a pull request may close this issue.

4 participants