goby-lang / goby

Goby - Yet another programming language written in Go
MIT License
3.49k stars 171 forks source link

Discussion: Goby's classes can be `include`d or `extend`ed into other classes #611

Closed hachi8833 closed 6 years ago

hachi8833 commented 6 years ago

This looks unintentional behavior: not so harmful so far. I feel that disabling this is better. How about this?

# This works in Goby
class Foo
  def fee
    99
  end

  def self.fou
    88
  end
end

class Vaa
  def vez
    77
  end

  def self.voo
    66
  end
end

class Bar
  include Foo
  extend Vaa
end

Bar.new.fee     # instance method via `include`
#=> 99
Bar.vez         # class method via `extend`
#=> 77

# The rest combinations cause errors as expected:
Bar.fee         # UndefinedMethodError: Undefined Method 'fee' for Bar
Bar.fou         # UndefinedMethodError: Undefined Method 'fou' for Bar
Bar.new.fou     # UndefinedMethodError: Undefined Method 'fou' for <Instance of: Bar>

Bar.voo         # UndefinedMethodError: Undefined Method 'voo' for Bar
Bar.new.voo     # UndefinedMethodError: Undefined Method 'voo' for <Instance of: Bar>
Bar.new.vez     # UndefinedMethodError: Undefined Method 'vez' for <Instance of: Bar>

This causes and error in Ruby when defining Bar

# include
TypeError: wrong argument type Class (expected Module)
# extend
TypeError: wrong argument type Class (expected Module)
st0012 commented 6 years ago

IMO we should forbidden this kind of behavior, cause it'll be very confusing

st0012 commented 6 years ago

@goby-lang/maintainers any other opinion?

64kramsystem commented 6 years ago

Definitely inclusion/extension should be reserved to modules.

st0012 commented 6 years ago

ok I'll open another issue to forbidden this and closing this