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

fix function isdef #173

Closed
wants to merge 1 commit into from
Closed

fix function isdef #173

wants to merge 1 commit into from

Conversation

omlins
Copy link

@omlins omlins commented Dec 22, 2021

fixes #172


islongdef(ex) = @capture(ex, function (fcall_ | fcall_) body_ end)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way of checking if an expression is a long function definition is taken from here:
here

However, it seems to me that the or statement has no effect and the following would be equivalent:

islongdef(ex)  = @capture(ex, function fcall_ body_ end)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wonder about the |... odd...

Independently of that, maybe also change https://github.com/FluxML/MacroTools.jl/blob/master/src/utils.jl#L298-L300 to use islongdef?

Copy link
Collaborator

@cstjean cstjean Dec 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, indeed, 69f5811 shows that I'm the one who changed a more complex | expression to this (fcall_ | fcall_). I'm going to guess that I did that purely because this is complex code and I wanted to play it safe.

Long story short: please try it without the |, and if all tests pass, we'll keep it like that.

@omlins
Copy link
Author

omlins commented Dec 22, 2021

@cstjean, can you review and merge this?

@cstjean
Copy link
Collaborator

cstjean commented Dec 27, 2021

It's on my todo list, but it may take a few more weeks, with covid... Sorry

Copy link
Collaborator

@cstjean cstjean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add negative examples to the test suite for isdef, to show that this PR is more correct? Particularly interested in function foo end (should that be a function definition? I think not), but others would be nice too.

@cstjean
Copy link
Collaborator

cstjean commented Dec 30, 2021

should that be a function definition?

We should probably check the docs. I'd call it function declaration, but if the docs say otherwise...

@matthias314
Copy link

matthias314 commented Jul 30, 2023

The proposed function

islongdef(ex)  = @capture(ex, function (fcall_ | fcall_) body_ end)

seems to work (although I don't fully understand why). Replacing (fcall_ | fcall_) by fcall_ would lead to a syntax error both in Julia 1.9 and 1.10.0-beta1.

However, I think there is a problem with isshortdef: I've tried definitions of the form

ex1 = :( function f(x,y) x+y end )
ex2 = :( function (x,y) x+y end )
ex3 = :( f(x,y) = x+y )
ex4 = :( (x,y) -> x+y )

plus variants with argument type annotations, return type annotations and where clauses. They are all accepted by splitdef. Moreover, longdef preserves ex1 and ex2 and converts ex3 and ex4 to ex1 and ex2, respectively. shortdef preserves ex3 and ex4 and converts ex1 and ex2 to ex3 and ex4, respectively. islongdef is true for ex1 and ex2, but isshortdef is true only for ex3. I think it would make sense for isshortdef to return true also for ex4.

@cstjean
Copy link
Collaborator

cstjean commented Aug 24, 2023

Right - those would all be good test cases for whoever takes up the baton on this PR.

@matthias314
Copy link

I could give it a try. Do you insist on using @capture? For example, I think one could simply say

islongdef(ex) = isexpr(ex, :function)

Of course, this doesn't check whether ex is well-formed, but the version with @capture doesn't do this completely, either.

@cstjean
Copy link
Collaborator

cstjean commented Sep 5, 2023

As long as the tests have good coverage, I don't think we need to worry about malformed expressions. isexpr is fine.

@cstjean cstjean mentioned this pull request Dec 17, 2023
@cstjean cstjean closed this in #199 Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

isdef() seems to return true in any case
4 participants