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

const Foo=int compiles; is that legal? what does it mean? #8610

Closed
timotheecour opened this issue Aug 11, 2018 · 9 comments · Fixed by #10454
Closed

const Foo=int compiles; is that legal? what does it mean? #8610

timotheecour opened this issue Aug 11, 2018 · 9 comments · Fixed by #10454
Labels

Comments

@timotheecour
Copy link
Member

timotheecour commented Aug 11, 2018

EDIT: is that the syntax to get a const typedesc variable ? where is that documented?

const Foo=int
echo Foo is int # true
echo int is Foo # Error: type expected
@metagn
Copy link
Collaborator

metagn commented Aug 11, 2018

It's equivalent to a typedesc value in the VM.

import macros

const Foo = int

macro foo(name: untyped): untyped =
  newTree(nnkVarSection, newIdentDefs(name, getType(Foo), newLit(5)))

foo(bar)
echo bar * 3 # 15

I don't think it's documented though it sometimes would be expected to work.

@Araq
Copy link
Member

Araq commented Aug 13, 2018

It's not legal.

@Araq Araq added Severe and removed question labels Aug 13, 2018
@ghost
Copy link

ghost commented Aug 16, 2018

Related?

proc test(a = int, string = string) =
  echo 1

test()

@GULPF
Copy link
Member

GULPF commented Aug 16, 2018

@Tim-St No, that's a default value of a typedesc argument.

import typetraits
# Same as 'proc test(a: typedesc = int): string = $a'
proc test(a = int): string = $a
doAssert test() == "int"
doAssert test(string) == "string"

@ghost
Copy link

ghost commented Aug 16, 2018

Ok, so If I understood correctly, const Foo = int should not be legal but
proc test(Foo = int) = discard should be legal?
Now one could argue, Foo in proc is not const, but then why does var Foo = int not work?

@dom96
Copy link
Contributor

dom96 commented Aug 16, 2018

Now one could argue, Foo in proc is not const, but then why does var Foo = int not work?

Because procedures can be generic, variables can't.

@ghost
Copy link

ghost commented Aug 16, 2018

But const Foo = int should be illegal and var Foo = int is already illegal and also a proc param (afaik) is always var or const. So I don't really understand why it should be allowed in the context of proc arguments when it's disallowed everywhere else, do you have an example where it makes sense to allow it in a proc but disallow it everywhere else?

Edit:
Well maybe for something like this

proc test(a = typedesc) =
  when a is int:
    echo 123
  else:
    echo "abc"

But still I think it is better to have it consistent.

Edit 2:
Ok, the style like in the proc is used in the nim compiler, so I assume it's legal syntax.

@Clyybber
Copy link
Contributor

@Tim-St types != values. Typedescs bridge that gap, and allow types to be used as compile-time values. That only makes sense for proc arguments.

@mratsim
Copy link
Collaborator

mratsim commented Aug 16, 2018

As I said in #8657. const Foo=int should be illegal at all time. type Foo=int should cover the same use-cases without special casing const for typedesc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants