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

Feature request: Allow type to return void #7397

Closed
mratsim opened this issue Mar 23, 2018 · 8 comments
Closed

Feature request: Allow type to return void #7397

mratsim opened this issue Mar 23, 2018 · 8 comments

Comments

@mratsim
Copy link
Collaborator

mratsim commented Mar 23, 2018

Currently you can alias the void type but the type proc cannot return void, it's a compile-time error.
There might be a good reason for it in that case it should be mentionned in the docs, otherwise I think it is a useful feature, here is a related post by @cooldome on the forum.

import macros,typetraits


type v = void # This works
echo v.name

type w = type((echo "foo"))  # This fails

# I want to followup with "when w is void:"

# Use case, so that we can `when test_type is void`
# in the following template that is used often to iterate
# and transform sequences (or any collection)
template test(b: seq[int], loopBody: untyped): typedesc =

  template test_type(): untyped =
    type((
      block:
        var x{.inject.}: type(items(b));
        loopBody
      ))

  when compiles(test_type()):
    # `when test_type() is void:` would be better I think
    test_type()
  else:
    void

let a = @[0, 1, 0, 1]

echo test(a, x.bool).name      # bool
echo test(a, echo x.bool).name # void
@mratsim
Copy link
Collaborator Author

mratsim commented Mar 23, 2018

Also this would allow to distinguish between those two cases:

Void type

let a = @[1, 2, 3]
let b = @[true, true, false]

type((
  block:
    var x: type(items(a))
    var y: type(items(b))
))

Statement with no type

let a = @[1, 2, 3]
let b = @[true, true, false]

type((
  block:
    var x: type(items(a))
    var y: type(items(b))
    if y:
      x
))

@krux02
Copy link
Contributor

krux02 commented Oct 13, 2019

I agree, that this is a good idea.

@Araq
Copy link
Member

Araq commented Oct 15, 2019

When I looked at void's problems I wanted to erase it from Nim altogether, it's that bad.

@krux02
Copy link
Contributor

krux02 commented Oct 15, 2019

You want to force every function to return a value?

@andreaferretti
Copy link
Collaborator

It could be a type like any other. Scala, for instance, has a type Unit, whose only member is the object denoted by the literal (). As such it carries no information, and it could be deleted behind the scenes, but treated like any other type elsewhere

@mratsim
Copy link
Collaborator Author

mratsim commented Oct 16, 2019

It can be called, void, Empty or NoneType but fundamentally I don't see how removing it works for a typical program or testing the absence of return values.

@Araq
Copy link
Member

Araq commented Oct 16, 2019

See nim-lang/RFCs#508 for details.

@metagn
Copy link
Collaborator

metagn commented Jan 16, 2022

This is now implemented (since 1.6), #17807

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

No branches or pull requests

6 participants