The actual need for an Object#to_b is that there's no way to make your
own custom object that will behave in a false/nil manner.
I can't think of a usecase where I'd want to put extra information into
a nil/false object instead of using exceptions, but maybe one of those
people that keep asking about subclassing FalseClass and NilClass can
explain their use-cases.
Unless somebody can come up with a decent use-case, I'd say that we just
change the if evaluations to also allow subclasses of FalseClass and
NilClass (which will all be just singletons as they don't have new). Or
change if to look at nil? to determine whether it's true or not.
···
-----Original Message-----
From: Austin Ziegler [mailto:halostatue@gmail.com]
Sent: Thursday, 17 November 2005 8:50 AM
To: ruby-talk ML
Subject: Re: Object#to_b
On 11/16/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
> Currently, as far as I know, `if' and `unless' merely check if an
> object is of either types NilClass or FalseClass. This seems to
> contradict the duck-typing paradigm. I propose that we implement a
> method Object#to_b that is called by `if' and `unless'. It
should return a boolean value.
> That way, a developer can decide whether or not an object should
> evaluate to true or false.
> One problem I see with this proposal is that it seems like a lot of
> people check if a variable has been set by writing `if var
...'. This
> change would require that people wrote `unless var.nil?', which I
> personally find more correct as well.
This would break a lot of my code, personally. I used to use
"unless var.nil?" but it is easier to say:
if var and var.foo
than:
if (not var.nil?) and var.foo
I'm not *quite* sure what the "unless" version of the test would be:
unless var.nil? or var.foo.nil?
Not quite what I want. I think that #to_b would be
problematic, especially in the example that you gave, since
it's more expressive to
say:
if conn.open?
than:
if conn
which implies you're checking to see if conn is really there.
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################
I want a simple test for presence of bar, so return nil from
Foo#bar if there is no associated object:
if foo.bar
# ...
end
I also want to treat Foo#bar as a reflection of the association
so I can have meta-methods for querying the column type, creating
objects, performing scoped queries, etc:
This usage feels very expressive and is possible for one-to-many
and many-to-many associations because acting like an Array is not
a problem. But for one-to-one associations, we must act like nil,
which is impossible due to RTEST doing direct object comparison.
to_b is one way to do it, but I think it is too disruptive.
I would prefer to define #nil? and have RTEST call it.
Best,
jeremy
···
On Nov 16, 2005, at 4:48 PM, Daniel Sheppard wrote:
The actual need for an Object#to_b is that there's no way to make your
own custom object that will behave in a false/nil manner.
I can't think of a usecase where I'd want to put extra information into
a nil/false object instead of using exceptions, but maybe one of those
people that keep asking about subclassing FalseClass and NilClass can
explain their use-cases.
Unless somebody can come up with a decent use-case, I'd say that we just
change the if evaluations to also allow subclasses of FalseClass and
NilClass (which will all be just singletons as they don't have new). Or
change if to look at nil? to determine whether it's true or not.
The actual need for an Object#to_b is that there's no way to make your
own custom object that will behave in a false/nil manner.
I don't know that this is a good or useful thing. I think that it would
decrease the overall clarity of Ruby code.
I can't think of a usecase where I'd want to put extra information
into a nil/false object instead of using exceptions, but maybe one of
those people that keep asking about subclassing FalseClass and
NilClass can explain their use-cases.
And I think that if they do, they'll be directed to different possible
solutions.
Unless somebody can come up with a decent use-case, I'd say that we
just change the if evaluations to also allow subclasses of FalseClass
and NilClass (which will all be just singletons as they don't have
new). Or change if to look at nil? to determine whether it's true or
not.
Hm. No, I don't think that either is good. I would suggest that neither
FalseClass nor NilClass should meaningfully have subclasses and that if
should *not* look at #nil?. I don't think that there is a good case to be found
for this at all.
-austin
···
On 11/16/05, Daniel Sheppard <daniels@pronto.com.au> wrote:
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca
On Thu, Nov 17, 2005 at 10:25:03AM +0900, Jeremy Kemper wrote:
On Nov 16, 2005, at 5:11 PM, Yukihiro Matsumoto wrote:
>In general, such attempt to define new false values is not worth the
>performance cost we all have to pay.
I would not like new false values, but I would like to be able to proxy
the existing false values. Especially nil.
I would not like new false values, but I would like to be able to
proxy the existing false values. Especially nil.
Ah, fudge. I hadn't thought of that. That is one place where
lazy.rb falls down.
promise { false } will always be true
(unless you use an explicitly force()'d value.)
What is the cost [of allowing NilClass and FalseClass subclasses]?
I don't see any way that it could be as efficient as the current
setup, where nil and false are immediate values and are checked
with a simple bitmask.
I don't see any way that it could be as efficient as the current
setup, where nil and false are immediate values and are checked
with a simple bitmask.
Could they get special object_id's that are easy to check?
Right: I had to dig as far as RTEST when I tried to act like nil
to understand why it did not work. I was happy to see that it's
very fast but was unhappy to discover the "line in the sand" between
dynamic, expressive Ruby and its concern for raw performance.
There is a Moore's law for one but not the other.
Has anyone measured the cost of doing boolean tests the Ruby way?
Clearly it will be much slower as a microbenchmark, but perhaps the
overall cost for most Ruby programs is low enough to tip the balance.
And if not now, perhaps in one year.
jeremy
···
On Nov 17, 2005, at 6:49 AM, mental@rydia.net wrote:
Quoting Jeremy Kemper <jeremy@bitsweat.net>:
I would not like new false values, but I would like to be able to
proxy the existing false values. Especially nil.
Ah, fudge. I hadn't thought of that. That is one place where
lazy.rb falls down.
promise { false } will always be true
(unless you use an explicitly force()'d value.)
What is the cost [of allowing NilClass and FalseClass subclasses]?
I don't see any way that it could be as efficient as the current
setup, where nil and false are immediate values and are checked
with a simple bitmask.
> I don't see any way that it could be as efficient as the
current
> setup, where nil and false are immediate values and are checked
> with a simple bitmask.
Could they get special object_id's that are easy to check?
That means we get 24 bits worth of symbols, 31 (signed) bits of
fixnums, and 31-bit pointers to objects (so objects must be aligned
on two-byte boundaries).
I think one of the interesting/useful features of Ruby is its
handling of boolean values. In thinking about the original post,
I came across a nice discussion of nil, null, and false in object
oriented languages, in particular Smalltalk and Objective-C.
I really like the null object pattern but I'm not sure I'm ready
to fully embrace it with a generalized null object and even then I'm
not sure how I think that should fit with nil/false.
My hunch is that either you have a generalized null object that is
considered false or you don't have it. The idea of having user-defined
false objects just seems to be a opening a Pandora's box of issues.
Gary Wright
···
On Nov 17, 2005, at 3:04 PM, Jeremy Kemper wrote:
Right: I had to dig as far as RTEST when I tried to act like nil
to understand why it did not work. I was happy to see that it's
very fast but was unhappy to discover the "line in the sand" between
dynamic, expressive Ruby and its concern for raw performance.