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

Let method_missing also generate a def #3610

Merged
merged 1 commit into from
Dec 1, 2016
Merged

Conversation

asterite
Copy link
Member

Fixes #3602

With this, the code generated by method_missing can also be a def, so one can control whether it captures a block, the block type, uses forall, etc. Example like this one:

class Bla
  def add(method, &block : -> U) forall U
    puts "added #{method} #{block}"
  end

  macro method_missing(call)
    def {{call.name}}(&block : -> U) forall U
      add({{call.name.stringify}}, &block)
    end
  end
end

bla = Bla.new

bla.some1 { 1 }
bla.some2 { 2 }

Output:

added some1 #<Proc(Int32):0x1063173a0>
added some2 #<Proc(Int32):0x1063173b0>

@kostya
Copy link
Contributor

kostya commented Nov 30, 2016

this is little strange because this is generate def and no call for it, it called by default?

@asterite
Copy link
Member Author

The way method_missing works (and this is how it always worked) is that, when triggered (method not found), the body of method_missing is expanded and then a method that matches that call is created and added to the object. In that way only a few expansions are needed. For example:

# This will trigger method_missing, a `some_method` will be generated and added
foo.some_method

# Now the method already exists in the object, and it's also typed, so it's fast.
# No need to re-expand method_missing and to type check again
foo.some_method
foo.some_method
foo.some_method

So this PR makes it a bit more clear that what is actually going on is that method_missing generated a method, it's not a call replacement.

I'll add all of this to the docs. In any case I'll need to update them after this is merged :-)

@asterite asterite added this to the 0.20.1 milestone Dec 1, 2016
@asterite asterite merged commit 6b098eb into master Dec 1, 2016
@asterite asterite deleted the feature/method_missing_def branch December 13, 2016 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants