[Kde-bindings] Re: weird behaviour of some classes' #text method in korundum

Stefano Crocco stefano.crocco at alice.it
Tue Feb 1 09:11:20 UTC 2011


On Monday 31 January 2011 17:43:05 Arno Rehn wrote:
> On Monday 31 January 2011 10:29:18 Stefano Crocco wrote:
> > Hello to everyone,
> > I just found out what I consider a bug. There are some classes with a
> > #text method which behaves strangely. Take for example the following
> > code
> > 
> > require 'Qt4'
> > app = Qt::Application.new []
> > w = Qt::Label.new; p w.text # => nil
> > w.text = ''; p w.text # => nil
> > w.text = 'xyz'; p w.text # => "xyz"
> > w.text = ''; p w.text # => ""
> 
> Yes, this is expected behaviour. The QString marshaller takes
> QString::isNull() into account (see the Qt docs for that).

I understand this. However, on the Qt docs, it also says that "All functions 
except isNull() treat null strings the same as empty strings". In ruby, 
however, nil and an empty string behave in a very different way.
> I really don't know whether this makes sense or not. For example, you can
> assign 'nil' to any QString property. When you do
> 
> w.text = nil
> w.text
> 
> should it return nil or ""?  From a C++ this all makes sense, but I'm not
> sure though in how far we want to reflect that behaviour in Ruby.
> Any thoughts?

In my opinion, nil should be returned only in situations where some 
"exceptional" event has occurred (for example it makes sense for 
Qt::InputDialog#get_text to return nil if the user presses the Cancel button). 
It may also make sense to return nil in cases like KTextEditor::Document#line 
if the line number given as argument is greater than the number of lines in 
the document.

What I think is totally unexpected is to have nil returned from Qt::Label#text 
(or the #text method of similar classes), expecially if I explicitly set the 
text to an empty, not invalid, string. As I wrote in my other mail:

w = Qt::Label.new
w.text # => nil
w.text = ''
w.text # => nil

The first nil could be understandable, because I've set no text for the label, 
but the second, I think, is simply wrong. In line 3, I explicitly set the 
label's text to an empty string, not to an invalid one. Besides, the above is 
inconsistent with the following:

w.text = 'xyz'
w.text = ''
w.text # => ""

Why should the value returned by #text after setting the text to an empty 
string depend on whether I had previously set it to a non-empty string or not?

Wrapping up my thoughts, I think that deciding whether to return nil whenever 
the C++ Qt or KDE returns a null string is not a wise decision. Since in C++ a 
null string works exactly as an empty string (except for isNull(), of course), 
they're free to make subtle distinctions like: if QLabel has been created 
without setting its text, QLabel::text returns a null string, while it returns 
an empty string if the text has been initialized to an empty string. In ruby, 
as nil and an empty string are two very different things, you can't make so 
subtle choices. Also, remember than people using QtRuby/korundum will still 
look at the C++ documentation, which speak of returning a string. Also, QtRuby 
usually uses nil where in C++ you use null pointers, not for strings.

Thanks for the attention

Stefano



More information about the Kde-bindings mailing list