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

super and method overwrite mechanism design #594

Open
st0012 opened this issue Feb 13, 2018 · 6 comments
Open

super and method overwrite mechanism design #594

st0012 opened this issue Feb 13, 2018 · 6 comments

Comments

@st0012
Copy link
Member

st0012 commented Feb 13, 2018

  • We should support method overwritten and monkey-patching
  • All methods (public or private) can do this
  • We shouldn't support Ruby's super keyword. But we can
    • Support something like super_from(GrandClass) to let users explicitly specify which ancestor's method they super to
    • Not supporting super-like mechanism at all
@64kramsystem
Copy link
Member

We shouldn't support Ruby's super keyword. But we can

Can you elaborate a bit on this one? Without super(), how do inherited methods invoke the ancestor, in particular, initialize()?
For the initialize() case, there is really no need for, say, super_from(), as invocation will go up the hierarchy.

Also, I'm a bit confused about the relationship between super and monkeypatching, as they apply to different areas (if I've understood well).

@hachi8833
Copy link
Member

Is the monkey-patching moderate like Ruby's refinement?

@st0012 st0012 changed the title super and monkey-patching mechanism design super and method overriden mechanism design Feb 14, 2018
@st0012 st0012 changed the title super and method overriden mechanism design super and method overwrite mechanism design Feb 14, 2018
@st0012
Copy link
Member Author

st0012 commented Feb 14, 2018

@saveriomiroddi @hachi8833 Change the title to be method overwriting, does that look better?

Can you elaborate a bit on this one? Without super(), how do inherited methods invoke the ancestor, in particular, initialize()?
For the initialize() case, there is really no need for, say, super_from(), as invocation will go up the hierarchy.

class GrandClass
  def initialize(foo)
    @foo = foo
  end
end

class ParentClass < GrandClass; end

module Bar
  def initialize(foo)
    @foo = foo + 10
  end
  # some other methods
end

class ChildClass < ParentClass
  include Bar
  def initialize(foo)
     super_from(GrandClass, foo) # This will skip the Bar's initialize
  end
end

I think this gives users a more clear view and control about the method inheritance.

@64kramsystem
Copy link
Member

64kramsystem commented Feb 15, 2018

So, the term exact is method overriding.

I don't think specific support for module overriding initialize() should be added.

The reason is that modules are used for composition; such override would interfere with inheritance.

Interestingly, this behavior could be considered multiple inheritance, which is not a very "appreciated" design (I've personally never seen it).

This is a silly example of why modules should not override a class initializer:

class Car
  def initialize
    start_engine
  end
end

module TopOpeningDoors
  def initialize
    open_doors
  end
end

class Ferrari < Car
  include TopOpeningDoors

  def initialize
    super_from(TopOpeningDoors) # won't start_engine

    start_turbines
  end
end

@st0012
Copy link
Member Author

st0012 commented Feb 16, 2018

@saveriomiroddi I used initialize is because you mentioned it earlier. I think initialization override can be another topic actually. Let me give you a different example:

class User
  def to_xml
   serialize(:email, :name)
  end
end

class Member < User
  def to_xml
   serialize(:email, :name, :member_id)
  end
end

class NullMember < Member
  def to_xml
    super_from(User)
  end
end

NullMember.new.to_xml #=> will only have email and name field

@64kramsystem
Copy link
Member

Ah! I see. Adding doesn't hurt 😄 👍

I think modules overriding should be taken into account as well (eg. the case where Member#to_xml is defined in another module, and not in Member), but maybe I'm stating the obvious 😄.

@st0012 st0012 added this to the version 0.2.0 milestone Feb 14, 2020
@st0012 st0012 self-assigned this Feb 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants