[swift-evolution] [Pitch] Never as a bottom type

André Videla andre.videla at gmail.com
Mon May 15 03:21:16 CDT 2017


> If you mean does this currently compile:

let f:Optional<Never> = nil
let g:[Never] = []

Of course it compiles, but it does not if you remove the type annotations.
My point was that, with a bit of work, you could remove those annotations
and still typecheck

> If Never becomes a bottom type, a [Never] or Never? hypothetically could
be passed into any function which accepts an array or optional of any type
because of covariance and value types, but would always be empty/none
version of that type. I can’t think of a practical reason to do this
It could be useful to mitigate the absence of GADT in this simple case:

let value: Either<Int, String> = .left(3) // TypeChecks

let value: Either<Int, Never> = .left(3) // TypeChecks

let value = .left(3) // Does not typecheck

The third line could be inferred as the second. And using the property you
are mentioning (passing values with Never types parameters to functions
with covariant argument) you could very simply do

func takeEither(e: Either<Int, String>) { … }

takeEither(.left(3))

2017-05-15 8:44 GMT+02:00 David Waite <david at alkaline-solutions.com>:

>
> On May 14, 2017, at 11:44 PM, André Videla via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Joe also referred to the following model in a thread about Typed Throws.
>
>   () -> () == () throws Never -> ()
>   () throws -> () == () throws Error -> ()
>
> That makes sense! As a bottom type it would be perfectly fit for this.
>
> On 15 May 2017, at 03:01, Robert Widmann <devteam.codafi at gmail.com> wrote:
>
> Though our type lattice doesn’t necessarily have a bottom in the way
> you’re looking for, you can use Never-returning functions to inhabit a
> combinator that will do this for you.
>
> Very useful.
> What do you think of [] as [Never], or nil as Optional<Never>. Can the
> current inference and subtyping rules deal with that?
>
>
> If you mean does this currently compile:
>
> let f:Optional<Never> = nil
> let g:[Never] = []
>
> then yes. However, “nil” and “[]” in the above are literals, not types or
> type instances. They only are used to initialize an instance of a type that
> is ExpressibleByNilLiteral / ExpressibleByArrayLiteral.
>
> If Never becomes a bottom type, a [Never] or Never? hypothetically could
> be passed into any function which accepts an array or optional of any type
> because of covariance and value types, but would always be empty/none
> version of that type. I can’t think of a practical reason to do this,
> although I admit I’ve never used a language with a bottom type.
>
> -DW
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170515/8bc0b81f/attachment.html>


More information about the swift-evolution mailing list