[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: automatic conversions to character strings
From: |
John W. Eaton |
Subject: |
Re: automatic conversions to character strings |
Date: |
Fri, 13 Nov 2015 13:19:44 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 |
On 11/13/2015 12:01 PM, Olaf Till wrote:
On Fri, Nov 13, 2015 at 09:36:18AM -0500, John W. Eaton wrote:
I'm beginning to think that value extractors that do implicit conversions
are kind of evil.
What were the reasons for this concept of implicit conversions?
Long ago, I thought it would be useful to be able to ask an octave_value
object to produce a value of a specific type and that if a conversion
were available, to just do it. Now it seems that it's not so useful to
do this kind of thing blindly.
Also, in some cases Matlab does (or used to do) implicit conversions
from numbers to strings so it seemed useful to me to bury that concept
in the code that extracts values. For example, one conversion that I
believe Matlab still does without any warning is
['f', 111, 111] ==> 'foo'
but I think many other functions used to just accept strings as numbers.
Way back, I'm pretty sure that strings in Matlab really were double
precision matrices that had a special flag set to say that they were to
be displayed as characters according to their ASCII values.
Maybe just relying on (implicitly converting) value extractors in each
case was a kind of misusing them, and actually e.g. .is_string()
should indeed have be called first in many cases ...
I would rather avoid asking the type first. Instead, I prefer to write
std::string str = arg.string_value ();
and just have that throw an error if the extraction can't happen. The
thing I'm trying to do now is preserve (or eventually, add) better error
messages than just "error: wrong type argument 'cell'" for these
extractions and to do it in a concise and clear way. So I think writing
std::string str = arg.string_value ("foobar: argument must be a string");
is useful.
jwe