Structures and the = Operator

In Xojo the = operator does double duty as the assignment operator and a boolean comparison operator for datatypes.

This is both intuitive and makes sense as anything xojo can assign it should be able to compare their underlying bytes to determine if they are the same…

But I seem to have found a case where that is not true where I think it should be… When using structures.

Suppose you have structure called myStructure in which you have different data types (including arrays)

Consider this code:

Dim S1, S2 as myStructure S1 = S2 ' Legal If S1 = S2 Then DoSomething ' compiler Gives incompatible operator error If S1.StringValue(True) = S2.StringValue(2) Then DoSomething ' complier is happy

Is “If S1 = S2 Then” not being legal a bug, an oversight or by design?

It not working is certainly counter intuitive and I keep stumbling over it.

Also having S1.StringValue(True) = S2.StringValue(2) (with longer variable names) sprinkled throughout my code sure makes it less readable!

Please see this.

[quote=103262:@Karen Atkocius]If S1.StringValue(True) = S2.StringValue(2) Then DoSomething ’ complier is happy
[/quote]
S2.StringValue(2) will compile if S2 is a structure.

What did you see on that page that would apply to Karen’s question? I don’t see a reference to structures at all.

Exactly.

Should read: S2.StringValue(2) will NOT compile if S2 is a structure.

I’m sure Karen already knows what the LR says about it.

I had a typo in the post.

i MEANT To type:

S1.StringValue(True) = S2.StringValue(True)

The point was that one would logically EXPECT/INTUIT that “If S1 = S2 Then” would work

BTW for those that don’t know I’m not a beginner in this language… I have been using it for over 13 years.

Basically i was asking should I file a bug report or feature request and the likelihood of Xojo inc getting it to work. If they are philosophically opposed to it for some reason, there is little point in making feature request.

From 5 years ago <https://xojo.com/issue/9080> FR: Overload = or IS boolean Operators Or add Equals function for Structures

I actually submitted that (not Aaron) while writing the code i am now revisiting … I totally forgot I did it! Thinking about it I think the = operator should be overloaded

  • Karen

I would not expect an equality comparison to work on structures any more than I would expect it to work on classes
[h]You have used an operator that is not compatible with the datatypes specified[/h]

I WAS suprised to see that unlike a CLASS, the variable is not a pointer… otherwise S1=S2 would result in both pointing to the same structure… which does not seem to happen.

[quote=103300:@Dave S]You have used an operator that is not compatible with the datatypes specified

I WAS suprised to see that unlike a CLASS, the variable is not a pointer… otherwise S1=S2 would result in both pointing to the same structure… which does not seem to happen.

[/quote]

That is because structures are not classes (and can not contain classes). They behave like basic datatypes they are composed of for most things, they are always passed/assigned ByVal not ByRef in the framework. That is how they were designed in RB from when they were first added to the language. It was a conscious decision on REALSoftware’s part.

Because of how they are defined and work in Xojo (vs other languages) I think it should work. Given how they have been implemented in Xojo, allowing it could not possibly break any code.

Changing structures to work as you expect would (the variables being pointers) break code and would be against the philosophy of the framework.

Remember Xojo is not (insert other language name here). My expectation came from the Xojo implementation.

The initial reply was based on the face value of the original post. The assignment accepts “Any” data type while comparison does not accept “Structure” type, as documented and as observed in compiler.

Apologies for misunderstanding.

When I designed the ‘structure’ system, I couldn’t think of a comparison algorithm which was clearly fundamental and correct in all cases. Even plain equality is hard: do you compare byte-wise, or member-wise? Do you skip padding? What about byte order? Instead of just making something up, and confusing everyone who needed something different, I opted not to define a general comparison operator for structures at all.

It is therefore up to you to decide what “equality” means, between two structures you have described, and to work out how to order them, if you need to sort a list. The language does not make that decision for you.

[quote=104260:@Mars Saxman]When I designed the ‘structure’ system, I couldn’t think of a comparison algorithm which was clearly fundamental and correct in all cases. Even plain equality is hard: do you compare byte-wise, or member-wise? Do you skip padding? What about byte order? Instead of just making something up, and confusing everyone who needed something different, I opted not to define a general comparison operator for structures at all.

It is therefore up to you to decide what “equality” means, between two structures you have described, and to work out how to order them, if you need to sort a list. The language does not make that decision for you.[/quote]

Mars,

It seems to me you are thinking in terms of structure variables of any mixture of arbitrary structure definitions. I am talking about comparing 2 structure variables that have the same structure definition (DIMed as the same structure) and ONLY that.

In that case I think your concerns go away. In that case equality should mean being absolutely the same, so it seems to me bytewise is just fine. As far as I can see byte order has no bearing then. On any given platform, in memory structure variables DIMed to the same type SHOULD only be equal if and only if the bytes are EXACTLY the same, no? I would be fine with JUST that…

But even if there are ambiguities with comparison across structure definitions , bytewise would still be fine if documented… (and is likely the most common need) . To me I see it little different in principle from comparing strings of NIL encoding. (Which is what I am doing now- this could just be much faster)

BTW nice to see you here! :wink:

  • Karen

Karen, you can get the stringValue of structure and use strcomp function for a binary comparison.

Don’t even need stringComp… The Strings returned from structures have nil encoding so any comparison is bytewise… and that is what I do now.

BUT you have the overhead of 2 function calls and quite frankly doing:

If StructVar1.StringValue(true) = StructVar2.StringValue(true) Then ...

is messy and unintuitive for IMO no good reason.

Even if the two structures / classes are DIMed at the same time, how will the runtime know how to resolve the comparison later? You have to compare what they contain, not their pointers which is what S1 == S2 implies - are these two variables pointing to the same memory location.

typedef struct { int bufcount; /* # of chars in buffer */ int bufsize; /* size of buffer */ char *buf; /* pointer to base of buffer */ char *ptr; /* pointer to current position in buffer */ int io_blocksize; /* size of normal I/O block */ int EOF_flag; /* TRUE if End-of-File reached */ int b_errno; /* Set to "b_errno" to eliminate WIN32 issue */ } BUFIO;

If you want to compare contents, you have to decide which contents. It is more work, but we’d be in serious trouble - as Mars alluded - in figuring out which element of which structure was compared.

First in Xojo there is no such thing as structure pointer. They are treated very much like simple datatypes such as integers or doubles etc… At the level we can get to there is no indirection possible.

Second I think you are missing what I am saying. It’s NOT WHEN something is declared but what it is declared AS.

Say you have a structure named MyStruct

Dim S1 as MyStruct
Dim S2 as MyStruct

The complier then knows that for S1 and S2 to be equal the bytes of both have to be the same

I also think you an Mars are may be being too ‘geeky’ …

Structures are useful for other things besides declares. They create a data structure that has built in serialization - which is then useful for copy and pasting, dragging, writing to files etc. They can be defined in classes - Which classes can not be.

I felt this need when using them for things BESIDES declares.

In languages that allow you to define a data type (and that is what a structure really is) an = comparison is usually allowed.

Ah, in that case, my “geeky” side would say that you are using a structure for something other than what it is intended. Your mechanism of using the StringValue to compare the contents is a hack at best. Granted, you get the result that you are looking for, but that is an unintended and undefined consequence of that little extension that Mars provided.

Also, I don’t use them for declares, I use them to manage memory constraints that I’ve assigned myself. While I know that I have GBs of memory and TBs of disk space, I’m still a believer in the tenet that smaller is better.