goby-lang / goby

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

Discussion: duality between instance variables and attribute methods #488

Closed hachi8833 closed 6 years ago

hachi8833 commented 6 years ago

As you know, Ruby's #= methods from attr_accessor or attr_writer are pretty confusing when assigning:

class Foo
  attr_accessor :bar, :baz, :bud
  def initialize
    bar = "hoge"         # just creates a new local variable `bar`
    self.baz = "huga"    # needs a receiver  `self.` to get `#=` work
    @bud = "huge"        # works
  end
end

Foo.new.bar
#» nil
Foo.new.baz
#» huga
Foo.new.bud
#» huge

Essentially, Ruby has a duality: you sometime call the ones "instance variables" and sometime "attributes".

Goby behaviors the same as above for now.

I hope Goby could provide a syntax level solution for that. Which of the following is preferable?

Personally I prefer to C, next B because A still have an ambiguity among @bar and #bar and bar for internal use:

class Foo
  attr_accessor :bar
  def initialize
    @bar = "hoge"
  end
  def show
    puts bar      # reading @bar via `#bar`, which apparently looks reading a local variable
    puts @bar     # I feel this is preferable
  end
end

Foo.new.show
#=> hoge
#=> hoge

B is less excessive and would be nice for Ruby.

Any suggestions?

64kramsystem commented 6 years ago

Assuming this would be implemented, I'd personally prefer a warning. Definitely not A, which wouldn't solve the ambiguity problem.

st0012 commented 6 years ago

@hachi8833 this could be scanned by linter I think

hachi8833 commented 6 years ago

I'm changing my mind around this: I'd close this issue, ok?