Nim: deprecate typedesc in favor of type

Created on 16 Nov 2018  路  6Comments  路  Source: nim-lang/Nim

var x: int = 0
proc foo(T: type): var T = x
foo int
template/generic instantiation of `foo` from here
inim_1542371335.nim(4, 28) Error: type mismatch: got <int> but expected 'None'
Error messages

Most helpful comment

According to the typedesc reforms PR, proc(T: type) is now the same as proc(T: typedesc), isn't it? Why should it be forbidden again?

All 6 comments

please use correct markdown, otherwise I have to manually remove all those nim> directives in your snippet, like you have to do it now in my snippet, which btw solves your problem.

nim> var x: int = 0
nim> proc foo[T](_: typedesc[T]): var T = x
nim> foo(int) = 17
nim> echo x

This remains an issue though, because there should be an error message that forbids the use of type in the expression of the parameters.

According to the typedesc reforms PR, proc(T: type) is now the same as proc(T: typedesc), isn't it? Why should it be forbidden again?

proc(T: type) is now the same as proc(T: typedesc), isn't it?

Yes, the original example throws the same error regardless if one uses type or typedesc. Analogously, @krux02's example works whichever version you use.

I wasn't aware of that PR. I guess then typedesc should be deprecated in favor of type. I don't like it when things can be referred by different names. It is very confusing to me when I stumble upon a project just just uses a different set of aliases than I do.

This seems like a better test case, the original one is actually a closure which might brings yet other issues:

var x: int = 0

proc foo(T: type, x: var T): var T = x
foo(int, x)
Error: type mismatch: got <type int, int>
but expected one of:
proc foo(T: type; x: var type): var type
  first type mismatch at position: 2
  required type: var None
  but expression 'x' is of type: int

expression: foo(int, x)

No, we should deprecate type in favour of typedesc.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timotheecour picture timotheecour  路  3Comments

ghost picture ghost  路  4Comments

SolitudeSF picture SolitudeSF  路  3Comments

teroz picture teroz  路  3Comments

juancarlospaco picture juancarlospaco  路  3Comments