The new Never type

394 views
Skip to first unread message

John

unread,
Jun 13, 2020, 4:14:46 PM6/13/20
to Dart Misc
I noticed in the Changelog a note about the exit function in dart:io having its return type set to Never.

I see no documentation in the API docs for the latest dev release 2.9.0-15.0.dev explaining what the Never type.

What's the difference between it and void?

re:fi.64

unread,
Jun 13, 2020, 8:00:18 PM6/13/20
to misc
Afaik Never means that the function never returns, e.g. it ends the process or always throws an exception. void means that the function still returns, it just doesn't return a value.

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/a66623f8-699f-4459-973e-0fed26525d17o%40dartlang.org.

John

unread,
Jun 14, 2020, 7:53:12 PM6/14/20
to Dart Misc
I get the meaning of the word "never" being a return type. But i am asking about documentation for the Never class. Why not change void so it's not returning anything?


On Saturday, June 13, 2020 at 8:00:18 PM UTC-4, re:fi.64 wrote:
Afaik Never means that the function never returns, e.g. it ends the process or always throws an exception. void means that the function still returns, it just doesn't return a value.

On Sat, Jun 13, 2020, 3:14 PM John <aggi...@gmail.com> wrote:
I noticed in the Changelog a note about the exit function in dart:io having its return type set to Never.

I see no documentation in the API docs for the latest dev release 2.9.0-15.0.dev explaining what the Never type.

What's the difference between it and void?

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mi...@dartlang.org.

林作健

unread,
Jun 14, 2020, 8:14:49 PM6/14/20
to mi...@dartlang.org

I think it is intended to provide extra information to the compiler for the optimization purpose. Just like the no return attribute in GCC, the compiler will not link an edge for the statements after this function returns.

Beside the optimization purpose, the compiler is able to emit warning for the statements fall through the function. They will never be able to execute.





林作健
阿里巴巴
电话:057128223456-null
地址:广东-广州-广电平云广场
阿里巴巴 企业主页
信息安全声明:本邮件包含信息归发件人所在组织所有,发件人所在组织对该邮件拥有所有权利。
请接收者注意保密,未经发件人书面许可,不得向任何第三方组织和个人透露本邮件所含信息的全部或部分。以上声明仅适用于工作邮件。
Information Security Notice: The information contained in this mail is solely property of the sender's organization.
This mail communication is confidential. Recipients named above are obligated to maintain secrecy and are not permitted to disclose the contents of this communication to others.
------------------------------------------------------------------
发件人:John<aggi...@gmail.com>
日 期:2020年06月15日 07:53:11
收件人:Dart Misc<mi...@dartlang.org>
主 题:Re: [dart-misc] The new Never type
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/465efc7c-410c-496c-b5a9-d044edb31264o%40dartlang.org.

Erik Ernst

unread,
Jun 15, 2020, 3:50:01 AM6/15/20
to Dart Misc
On Mon, Jun 15, 2020 at 1:53 AM John <aggi...@gmail.com> wrote:
I get the meaning of the word "never" being a return type. But i am asking about documentation for the Never class. Why not change void so it's not returning anything?

The return type `void` does not mean that the function doesn't return anything (any function can be called dynamically, so every function must return some object), it means that the returned value is intended to be ignored, because no attempt was made to provide anything which makes sense for any particular purpose. It's usually null, but it could be any object whatsoever.

The type `Never` is the empty type, so when we have `Never myFunction() => throw 1;` the return type serves to say that this function doesn't return normally, ever. It will loop for ever, or it will throw an exception, but it won't just return in the normal way.

This is useful, for instance in the case where the flow analysis of a function can avoid some branches. For instance, `int f() { if (b) myFunction() else return 42; }` can pass the static analysis because it is known that `f` will return an actual `int` along one path, and it will never succeed in taking the other path all the way to the end of the function.

On Saturday, June 13, 2020 at 8:00:18 PM UTC-4, re:fi.64 wrote:
Afaik Never means that the function never returns, e.g. it ends the process or always throws an exception. void means that the function still returns, it just doesn't return a value.

On Sat, Jun 13, 2020, 3:14 PM John <aggi...@gmail.com> wrote:
I noticed in the Changelog a note about the exit function in dart:io having its return type set to Never.

I see no documentation in the API docs for the latest dev release 2.9.0-15.0.dev explaining what the Never type.

What's the difference between it and void?

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mi...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/a66623f8-699f-4459-973e-0fed26525d17o%40dartlang.org.

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/465efc7c-410c-496c-b5a9-d044edb31264o%40dartlang.org.


--
Erik Ernst  -  Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 København K, Denmark
CVR no. 28866984

Michael Thomsen

unread,
Jun 15, 2020, 4:14:46 AM6/15/20
to mi...@dartlang.org
Also, this is a new type not currently in a stable release. I logged a bug to make sure we get it documented: https://github.com/dart-lang/site-www/issues/2485

Lasse R.H. Nielsen

unread,
Jun 15, 2020, 8:47:29 AM6/15/20
to mi...@dartlang.org
Erik has explained what `Never` means.
It's worth stating that it is not a class, just a type.

Like `dynamic`, `void`, `FutureOr` and any function type, the `Never` type is not a class, and it has no interface that you can implement. It is a type that can be used in the Dart type system, rather like `dynamic`, except that `Never` is at the bottom of the type hierarchy instead of the top.

That also means that we have no good place to place documentation for it. It's a language feature with no declaration, so we can't put DartDoc on the declaration. Just like `dynamic`, kind-of like `FutureOr` except that we introduced a fake `FutureOr` declaration during the Dart 2.0 migration so that old tools could still see something.
Maybe we should do something similar for Never: Introduce a type declaration in `dart:core` that we can hang documentation offf, but which tools know to ignore.
(In the long run, maybe we could have external type declarations, like `external typedef Never;`).



On Mon, Jun 15, 2020 at 1:53 AM John <aggi...@gmail.com> wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/465efc7c-410c-496c-b5a9-d044edb31264o%40dartlang.org.


--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

aggi...@gmail.com

unread,
Nov 26, 2020, 2:19:38 PM11/26/20
to Dart Misc, Lasse Reichstein Holst Nielsen
I don't know if a fake 'Never' declaration is the solution. Maybe in the short term, but maybe DartDoc should have a way to document types like dynamic, FutureOf, and Never that isn't a workaround or "hack" of the existing system.

These are things that are unique to Dart and so they are important to include in the docs that it's buried in a page for Null Safety or something else.

Language Tour would be a good spot, though I always thought the index page for api.dart.dev would be a perfect spot to include the information found in the Language Tour. Something general, but not a tutorial.

Gives a good overview all within the generated docs.

Reply all
Reply to author
Forward
0 new messages