Forum: Ruby-core [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark)

Posted by Thomas Sawyer (7rans)
on 2011-12-19 20:58
(Received via mailing list)
Issue #5781 has been reported by Thomas Sawyer.

----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Open
Priority: Normal
Assignee:
Category:
Target version: 1.9.4


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Yui NARUSE (Guest)
on 2011-12-20 13:53
(Received via mailing list)
Issue #5781 has been updated by Yui NARUSE.

Status changed from Open to Assigned
Assignee set to Yukihiro Matsumoto
Target version changed from 1.9.4 to 2.0.0


----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category:
Target version: 2.0.0


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Yukihiro Matsumoto (Guest)
on 2011-12-20 15:05
(Received via mailing list)
Issue #5781 has been updated by Yukihiro Matsumoto.


It's mostly because semantics.  attr :a creates a method corresponding 
to an instance variable @a.  So naively, attr :a? tries to create an 
instance variables @a? which is not a valid name for a instance 
variable.

I don't want to allow instance variable names ending '?', just because ? 
is for predicates, not for variables.
The other option is removing '?' from instance variables.  But as far as 
I remember no one seriously proposed the idea before, and we haven't got 
consensus.

Matz.
----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category:
Target version: 2.0.0


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Jeremy Bopp (Guest)
on 2011-12-20 15:47
(Received via mailing list)
On 12/20/2011 08:03 AM, Yukihiro Matsumoto wrote:
>
> Issue #5781 has been updated by Yukihiro Matsumoto.
>
>
> It's mostly because semantics.  attr :a creates a method corresponding to an 
instance variable @a.  So naively, attr :a? tries to create an instance variables 
@a? which is not a valid name for a instance variable.
>
> I don't want to allow instance variable names ending '?', just because ? is for 
predicates, not for variables.
> The other option is removing '?' from instance variables.  But as far as I 
remember no one seriously proposed the idea before, and we haven't got consensus.

How about adding an attr_query method that otherwise works identically
to attr_reader but creates a method with a '?' on the end of the given
name instead?

For example, you could use it like this:

class Foo
  attr_query :bar
  attr_writer :bar
end

foo = Foo.new
foo.bar = true
foo.bar?         # => true
foo.bar = false
foo.bar?         # => false

-Jeremy
Posted by Anurag Priyam (Guest)
on 2011-12-20 17:06
(Received via mailing list)
On Tue, Dec 20, 2011 at 8:17 PM, Jeremy Bopp <jeremy@bopp.net> wrote:
> How about adding an attr_query method that otherwise works identically
> to attr_reader but creates a method with a '?' on the end of the given
> name instead?

I would suggest the name `predicate` instead.

class Foo
  attr :bar
  predicate :bar
end

Alternatively,

On Tue, Dec 20, 2011 at 7:33 PM, Yukihiro Matsumoto <matz@ruby-lang.org> 
wrote:
> The other option is removing '?' from instance variables. But as far as I 
remember no one seriously proposed the idea before, and we haven't got consensus.

Use `attr`, but if the variable name passed to `attr` contains a '?'
define a predicate corresponding to it too.

class Foo
  attr :bar?
  attr :baz
end

f = Foo.new
f.bar?  #=> nil
f.bar = 'meh'
f.bar    #=> 'meh'
f.bar?  #=> 'meh'

f.baz?  #=> NoMethodError

The predicate could be made to strictly return `true` or `false`
instead of simply behaving like an attribute reader.


The former is perhaps easier to implement, but I would like to use the
latter approach in my code.
Posted by Thomas Sawyer (7rans)
on 2011-12-20 19:52
(Received via mailing list)
Issue #5781 has been updated by Thomas Sawyer.


@jeremy Been down that road, and it's not as clean as you might expect. 
You end up needing two methods e.g. `attr_query_reader` and 
`attr_query_accessor` (`attr_query_writer` would be essentially 
meaningless). Moreover, adding additional attr methods tends to be one 
of those "Cambrian explosion" deals --there are vast variations people 
have devised. Check out Rails for examples. So I don't think it's a good 
precedence for core Ruby. In fact I've always thought it a bit 
unfortunate that #attr alone wasn't all we needed.

----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category:
Target version: 2.0.0


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Thomas Sawyer (7rans)
on 2011-12-20 20:00
(Received via mailing list)
Issue #5781 has been updated by Thomas Sawyer.


@matz In that case I would propose:

  attr :foo?
  #=> def foo?; @foo; end

  attr_reader :foo?
  #=>  def foo?; @foo; end

  attr_writer :foo?
  #=>  def foo=(x); @foo=(x); end

  attr_accessor :foo?
  #=> attr_reader :foo?; attr_writer :foo?

It's an open question as to whether #attr and/or #attr_reader should 
define the plan method too. Or if only true/false should be the return 
value. For the former, I do not think it matters much; maybe #attr just 
defines the "predicate" method and #attr_reader can define both? As to 
that later, I think it's better not to do boolean conversion. I believe 
Ara (or was it Austin?) made good arguments to that effect some years 
ago, reminding us that conditionals would function the same either way.

----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category:
Target version: 2.0.0


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Benoit Daloze (Guest)
on 2011-12-20 23:45
(Received via mailing list)
Issue #5781 has been updated by Benoit Daloze.


> So why not just allow: attr :foo?

I agree. I know many people wish for that too.

> matz: The other option is removing '?' from instance variables. But as far as I 
remember no one seriously proposed the idea before, and we haven't got consensus.

What do you mean by removing '?' from instance variables ?
As you said, '?' is already forbidden in instance variable names.

> Thomas Sawyer: It's an open question as to whether #attr and/or #attr_reader 
should define the plan method too.

I think the plain (bare) method should not be defined (to keep it as 
clean and simple as possible), and there should not be any conversion 
(which could lose information).

Also, since attr* :foo? does not conflict with current uses, I think 
it's fine to use the usual attr* methods.
----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category:
Target version: 2.0.0


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Joshua Ballanco (jballanc)
on 2011-12-21 20:28
(Received via mailing list)
Issue #5781 has been updated by Joshua Ballanco.


Perhaps one option to consider is to allow extra parameters specifying 
alternate names for the getters and setters (Obj-C does this for 
synthesized properties). Something like:

attr_accessor :foo, { :var => :bar, :getter => :is_barable? }, :baz
----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category:
Target version: 2.0.0


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by ko1 (Koichi Sasada) (Guest)
on 2012-10-26 23:32
(Received via mailing list)
Issue #5781 has been updated by ko1 (Koichi Sasada).

Target version changed from 2.0.0 to next minor

I changed target to next minor because no discussion on it.

----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781#change-31673

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Thomas Sawyer (7rans)
on 2012-12-23 19:44
(Received via mailing list)
Issue #5781 has been updated by trans (Thomas Sawyer).


=begin
I revisited this b/c in one of my projects it is much needed. To 
compensate, I created a special extension called (({attr_switch})),

    def attr_switch(name)
      attr_writer name
      module_eval %{
        def #{name}?
          @#{name}
        end
      }
    end

But it has the problem that the (({#source_location})) for the method 
created is in attr_switch's definition and not where attr_switch is 
called. And in my case that is a problem. Does anyone know if there is a 
way to tell it the (({source_location})) should be at caller[0]? I tried 
adding that info to (({#module_eval})) call, i.e.

  file, line = *caller[0].split(':')[0..1]
  module_eval %{...}, file, line.to_i

But it didn't work.

So bringing this back to this feature request. I, for one, still would 
very much like this feature. Sometimes it's just much more convenient. 
And I'd much rather it just worked out-of-box then me having to fuss 
with creating a custom attr method (and as I point out above, I can't 
even get it to work exactly the same).

I took a look at the relevant C code, (({rb_attr()})) in 
(({vm_method.c})), but I simply do not understand that code enough to 
adjust it myself. If I did, I would have submitted a patch for this 
already.

Given what I understand about the new ((*process*)) for changing Ruby, I 
guess I need a sponsor from core team or a core member of another 
implementation. Is that correct? If so, is anyone willing to back this?
=end

----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781#change-35033

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Posted by Thomas Sawyer (7rans)
on 2012-12-24 03:11
(Received via mailing list)
Issue #5781 has been updated by trans (Thomas Sawyer).


=begin
FYI,

  file, line = *caller[0].split(':')[0..1]
  module_eval %{...}, file, line.to_i

Actually this does work. My problem with #source_location stemmed from 
getting it from the (({attr_writer})) defined method. What I had to do 
was:

  def attr_switch(name)
    file, line = *caller[0].split(':')[0..1]
    module_eval %{
      def #{name}=(x)
        @#{name}=x
      end
      def #{name}?
        @#{name}
      end
    }, file, line.to_i
  end

In any case, still would be better to have `attr_accessor :x?` work.
=end

----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781#change-35042

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor


Pretty sure this has come up before, but I'd like to revisit b/c I don't 
understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the 
reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of 
potentially cleaner code.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.