Future<void>

1,691 views
Skip to first unread message

Sean Eagan

unread,
Feb 16, 2012, 5:35:07 PM2/16/12
to General Dart Discussion
I have found myself wanting to do something like the following on several occasions:

Future<void> someAsyncActionWhichDoesNotProduceAValue();

Currently you would have to do:

Future
Future<Null> // I assume this works, haven't tested

neither of which capture the intent.

Also, it's weird that I can use it as the return value of a getter, but not as the type of a field:

void get foo(); // legal
final void foo; // illegal

Would it make sense to make "void" just a normal type in the core lib, just like "Dynamic" ?

It should probably still remain reserved for now, but it would open it up to being unreserved at some point in the future.

Cheers, 
Sean Eagan

Alex Rickabaugh

unread,
Feb 16, 2012, 6:12:46 PM2/16/12
to Sean Eagan, General Dart Discussion
What is wrong with java.lang.Void?

Alex Rickabaugh

unread,
Feb 16, 2012, 6:13:08 PM2/16/12
to Sean Eagan, General Dart Discussion
Whoops, didn't realize this was the Dart list and not a Java list.

Gilad Bracha

unread,
Feb 16, 2012, 7:06:10 PM2/16/12
to Sean Eagan, General Dart Discussion
On Thu, Feb 16, 2012 at 2:35 PM, Sean Eagan <seane...@gmail.com> wrote:
I have found myself wanting to do something like the following on several occasions:

Future<void> someAsyncActionWhichDoesNotProduceAValue();

Yes, this idea has come up. 

Currently you would have to do:

Future

what I would recommend.
 
Future<Null> // I assume this works, haven't tested

Null is not available in user code, so it won't probably won't work. 

neither of which capture the intent.

Also, it's weird that I can use it as the return value of a getter, but not as the type of a field:

void get foo(); // legal
final void foo; // illegal

Would it make sense to make "void" just a normal type in the core lib, just like "Dynamic" ?

Normal types denote (roughly) a set of values.  Void indicates the absence of a value. We could view it as the empty set. Then it would be legal to have a variable have type void - logically it would be a variable that never holds a value. But in fact,  no such variable exists in Dart - there is always something there, if only null. 

Of course, Dart doesn't have functions that do not return a value. They will return null if nothing else. We use void as marker for the type system to help catch misuse of functions that are not intended to return a value (i.e., used only for side effects). If we allowed wider use of void, we would be supposedly marking variables that are not intended to be used. This is useless, but more regular. However, besides being useless, would it be harmful to allow it? Maybe, as people might use it and be confused. So we decided to be conservative.

I actually would prefer to do away with void altogether, but that's not something we'll pursue.

It should probably still remain reserved for now, but it would open it up to being unreserved at some point in the future.

Cheers, 
Sean Eagan



--
Cheers, Gilad

Bob Nystrom

unread,
Feb 16, 2012, 8:50:58 PM2/16/12
to Gilad Bracha, Sean Eagan, General Dart Discussion
On Thu, Feb 16, 2012 at 4:06 PM, Gilad Bracha <gbr...@google.com> wrote:

Normal types denote (roughly) a set of values.  Void indicates the absence of a value. We could view it as the empty set.

Isn't void in Dart right now closer to Unit than to the bottom type you're describing here? I'm actually not sure what the difference between Null and Void is in Dart, actually, so maybe there's a distinction I'm missing.
 
Then it would be legal to have a variable have type void - logically it would be a variable that never holds a value. But in fact,  no such variable exists in Dart - there is always something there, if only null. 

Of course, Dart doesn't have functions that do not return a value. They will return null if nothing else. We use void as marker for the type system to help catch misuse of functions that are not intended to return a value (i.e., used only for side effects). If we allowed wider use of void, we would be supposedly marking variables that are not intended to be used. This is useless, but more regular.

It's not entirely useless, though. Void has its uses in Java (with generics) so many of those would likely apply to Dart too, I think. (Of course, whether or not we should care enough about generics in Dart to make an issue of that is an open question. :) )

- bob

Jason Rosenberg

unread,
Feb 16, 2012, 9:16:45 PM2/16/12
to Bob Nystrom, Gilad Bracha, Sean Eagan, General Dart Discussion
Another issue currently with having a Future which is really waiting for a null return type, then the closure you pass to then still needs to take an argument, and it is useless, currently:

Future f = startSomethingAsyncThatHasNoReturnValue();
f.then((var voidObj) {
  // no use for voidObj in here (it has no value?)...Is it syntactically ok to refer to voidObj in here?
  print("async task is done");
});

Thus, it would be nice to be able to something like:

Future<Void> f = startSomethingAsyncThatHasNoReturnValue();
f.then(() {   // this syntax would only be allowed if the generic type of the Future is Void, or some such.
  print("async task is done");
});

Gilad Bracha

unread,
Feb 17, 2012, 12:47:19 AM2/17/12
to Bob Nystrom, Sean Eagan, General Dart Discussion
On Thu, Feb 16, 2012 at 5:50 PM, Bob Nystrom <rnys...@google.com> wrote:


On Thu, Feb 16, 2012 at 4:06 PM, Gilad Bracha <gbr...@google.com> wrote:

Normal types denote (roughly) a set of values.  Void indicates the absence of a value. We could view it as the empty set.

Isn't void in Dart right now closer to Unit than to the bottom type you're describing here? I'm actually not sure what the difference between Null and Void is in Dart, actually, so maybe there's a distinction I'm missing.

Recall that null is an object in Dart, just like everything else. Uppercase Null is a class - it is the class of the object null.  It induces the type Null, whose members are the instances of class Null. In practice, its only member is null since take care to canonicalize it.  We keep the class Null in the implementation, and you cannot get at it from other code.

Obviously, the type void is a different beast altogether. There are no values in the void type. And yes, you could instead equate it with Unit if you wanted to. Then you'd say it had one distinguished value, unit. But then, what kind of object would unit be? It doesn't have any methods, not even those that come from Object.  It seems better not to go that route.
 
Then it would be legal to have a variable have type void - logically it would be a variable that never holds a value. But in fact,  no such variable exists in Dart - there is always something there, if only null. 

Of course, Dart doesn't have functions that do not return a value. They will return null if nothing else. We use void as marker for the type system to help catch misuse of functions that are not intended to return a value (i.e., used only for side effects). If we allowed wider use of void, we would be supposedly marking variables that are not intended to be used. This is useless, but more regular.

It's not entirely useless, though. Void has its uses in Java (with generics) so many of those would likely apply to Dart too, I think. (Of course, whether or not we should care enough about generics in Dart to make an issue of that is an open question. :) )

I would have liked do away with void altogether. It's main justification is for detecting calls on methods that don't return a meaningful value.  

- bob




--
Cheers, Gilad

Ross Smith

unread,
Mar 20, 2012, 6:55:36 AM3/20/12
to General Dart Discussion
Hi guys,

I have opened an issue for this in the tracker as I could not find one
existing:

http://code.google.com/p/dart/issues/detail?id=2231&thanks=2231&ts=1332240786

I feel that this is lacking, especially in the context of generics, so
please star if you feel the same.

cheers,
-Ross

On Feb 17, 1:47 am, Gilad Bracha <gbra...@google.com> wrote:
> On Thu, Feb 16, 2012 at 5:50 PM, Bob Nystrom <rnyst...@google.com> wrote:
Reply all
Reply to author
Forward
0 new messages