Skip to content

x as T shorthand for T(x)::T ? #57857

Open
@StefanKarpinski

Description

@StefanKarpinski

It's become fairly common for people to write code like this:

n = Int(length(items))::Int

This is kind of verbose and redundant. I had always hoped that we might add type signatures for generic function such that the ::Int part is implied by the signature of Int(...) but that seems unlikely in the Julia 1.x timeframe. Perhaps a more pragmatic solution would be to have syntax sugar for the "convert and assert" pattern. We could have

n = length(items) as Int
# means this:
n = Int(length(items))::Int

Of course, in this case, I think you could just as well write n::Int = length(items) but that's not always doable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requests

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions