Skip to content

Long form anonymous functions don't support return type declarations #32557

@mbauman

Description

@mbauman

It would be nice if the long form anonymous functions would support return type declarations akin to named functions. That is, it'd be nice if the following converted and asserted the return type is T:

julia> function(x, y)::T
           return x*y
       end
ERROR: syntax: expected "(" in function definition

Note that this appears to work with one argument, but it's bugged and actually applying the type declaration to the argument instead of the function's return:

julia> f = function(x)::Int
           x/2
       end
#11 (generic function with 1 method)

julia> f(2.0)
ERROR: MethodError: no method matching (::getfield(Main, Symbol("##11#12")))(::Float64)
Closest candidates are:
  #11(::Int64) at REPL[41]:2
Stacktrace:
 [1] top-level scope at none:0

julia> :(function(x)::Int
                  x/2
              end)
:(function (x::Int,)
      #= REPL[47]:2 =#
      x / 2
  end)

(from https://discourse.julialang.org/t/controlling-the-type-of-anonymous-functions/26264).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorfeatureIndicates new feature / enhancement requestsparserLanguage parsing and surface syntax

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions