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

Comments

Projects
None yet
9 participants
@timotheecour
Copy link
Contributor

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
@hlaaftana

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented Aug 13, 2018

It's not legal.

@Araq Araq added High Priority and removed question labels Aug 13, 2018

@tim-st

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2018

Related?

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

test()
@GULPF

This comment has been minimized.

Copy link
Member

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"
@tim-st

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

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.

@tim-st

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2018

@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

This comment has been minimized.

Copy link
Collaborator

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.

timotheecour added a commit to timotheecour/Nim that referenced this issue Jan 25, 2019

@Araq Araq closed this in #10454 Jan 25, 2019

Araq added a commit that referenced this issue Jan 25, 2019

ThomasTJdev added a commit to ThomasTJdev/Nim that referenced this issue Jan 27, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.