I know that there are some gotchas when using xojo.core.dictionary after loading with ParseJSON, but I can’t figure this one out. I have a xojo.core.dictionary that is loaded with ParseJson. This comparison
if d.value(“aText”) = “someString” then
gives a TypeMismatchException (both the key and the value for aText are of type Text)
But this doesn’t throw the exception
dim s as string = d.value("aText")
if s = "someString" then
I can’t reproduce the problem with with Text that I create in code, e.g. the following does not throw an exception
dim t as Text = “aText”
if t = “aString” then
A bug that I should report, or something I should expect with the new framework?
I haven’t looked into this closely, but offhand it certainly looks like some kind of issue. At the very least, the error messages are absolutely unhelpful.
I have seen crazy stuff like this, too, with the Xojo.Core.Dictionary. It has to do with text literals somehow being different from a Text variable.
It’s enough to pull your hair out. If you have hair. Which I don’t. But it does make me swear at the Xojo engineers (sorry Joe) when I come across it. For this reason I’m not spending much time in the new framework.
“aText” and “someString” are only of type Text, if you are using the “Using Xojo.Core” clause. If not they would be of type String (unless it is an iOS project of course).
Yes, I do believe that there is some string/text issue going on here. While digging into this I came across another unexpected (at least to me) observation – .toString doesn’t seem to honor the string encoding. Example:
dim s as string = “test” // s is UTF-8
dim t as text = s.toText // t is UTF-16
[quote=268498:@Jonathan Ashwell]Yes, I do believe that there is some string/text issue going on here. While digging into this I came across another unexpected (at least to me) observation – .toString doesn’t seem to honor the string encoding. Example:
dim s as string = “test” // s is UTF-8
dim t as text = s.toText // t is UTF-16[/quote]
Dim dct As New Xojo.Core.Dictionary()
dct.Value("aKey") = "abc"
MsgBox dct.Value("aKey")
will “aKey” be a String or a Text? How can I enforce one of the two (without using a Dim statement) if not by a Using Xojo.Core clause?
[quote=268644:@Jeff Tullin]Non-technically, this is a mess.
How could anyone hoping to learn Xojo for the first time deal with that?[/quote]
One learning to ski won’t venture right away on the black track. Dictionaries are very powerful, and as such, have a learning curve. The idea that Xojo being for “ordinary people” they would be dispensed of acquiring enough knowledge to master certain concepts is at best naive.
These keys in a Xojo.Core.Dictionary are identical :
dim myKey as Text = "whatever"
NewFrameworkdico.value(mykey) = "something"
and
dim myKey as String = "whatever"
NewFrameworkdico.value(mykey.ToText) = "something"
But no need to use Xojo.Core.Dictionary and the Text data type to have this kind of subtleties. Here is an example with classic dictionary :
[code] Dim s as string = “whatever”
Dim s1 as string = DefineEncoding(“whatever”, Encodings.UTF16)
dim dic as new dictionary
dic.value(s) = “One”
dic.value(s1) = “Two”
system.DebugLog(dic.value(s)) // -> One
system.DebugLog(dic.value(s1)) // -> Two[/code]
So even between two strings if the encoding differ, they will be two different keys.