Skip to content

Argument destructuring doesn't work with callable structs #36572

Closed
@MasonProtter

Description

Consider the following iterable struct. We see that argument destructuring works as expected when Foo is the argument to a function:

struct Foo{T}
    a::T
    b::T
end
function Base.iterate(f::Foo, i=1) 
    if i == 1
        (f.a, 2)
    elseif i == 2
        (f.b, 3)
    else
        nothing
    end
end

apply((a,b)::Foo, x) = (a*x + b)/(b'x + a')

apply(Foo(1,2), 1+im)

#+RESULTS:
: 0.8461538461538461 - 0.23076923076923078im

but it fails if we try to make the struct itself callable and use destructing on the struct:

((a,b)::Foo)(x) = (a*x + b)/(b'x + a')

#+RESULTS:
:RESULTS:
# [goto error]
: syntax: "(a, b)" is not a valid function argument name around In[20]:1
: 
: Stacktrace:
:  [1] top-level scope at none:1
:END:

I think it'd be nice for consistency to allow this syntax.

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorcompiler:loweringSyntax lowering (compiler front end, 2nd stage)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions