Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mbauman opened this issue Jul 11, 2019 · 5 comments
Closed

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

mbauman opened this issue Jul 11, 2019 · 5 comments
Labels
bug Indicates an unexpected problem or unintended behavior feature Indicates new feature / enhancement requests parser Language parsing and surface syntax

Comments

@mbauman
Copy link
Member

mbauman commented Jul 11, 2019

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).

@mbauman mbauman added bug Indicates an unexpected problem or unintended behavior parser Language parsing and surface syntax feature Indicates new feature / enhancement requests labels Jul 11, 2019
@nathanielvirgo
Copy link

Probably a related issue: if you try and put type assertions on both the argument and the return type, it gives a syntax error:

julia> f = function(x::Int)::Int
ERROR: syntax: expected "(" in function definition

@JeffBezanson
Copy link
Member

Long form anonymous functions are one of my least favorite syntaxes :) In the example from discourse, there's a name anyway so it can be written as

function make_func()
	function f(x::Cint)::Cint
		return x*10
	end
end

The (x)::Int is unfortunately ambiguous; I'm not sure we can do anything about this.

@mbauman
Copy link
Member Author

mbauman commented Jul 11, 2019

I get that this is a super-overloaded form, and I get that we're free to parenthesize the names of arguments like (x)::Int, but don't we need an outer set of parentheses for the argument list itself? I'm missing the ambiguity.

@rfourquet
Copy link
Member

This is a dup of #23072.

@mbauman
Copy link
Member Author

mbauman commented May 4, 2020

Duplicate of #23072

@mbauman mbauman marked this as a duplicate of #23072 May 4, 2020
@mbauman mbauman closed this as completed May 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior feature Indicates new feature / enhancement requests parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

4 participants