Is it possible to make this function generic? #570
-
I'm trying to make a collector but I'm running into a problem where I can't make the function generic (it is only works when I specify the final type)
the error is
|
Beta Was this translation helpful? Give feedback.
Answered by
TimWhiting
Aug 9, 2024
Replies: 1 comment 1 reply
-
Ok, I found a way is to add div to type
but the question is, why they behavior differently? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You don't actually have to add
div
to the function argument type in this case. Just the result type.There is no change in behavior of the program. The
div
effect signifies that something can diverge (not terminate). It doesn't say that it must diverge, it is just saying that in pathological cases it might.However, in this particular case it seems to be an issue with local variable inference. You can switch it to explicitly use a heap allocated reference, and it can infer a non-divergent type: