fbpx
× Visual Objects

Please use this forum to post questions about Visual Objects and Vulcan.NET

X==nil

More
1 year 6 months ago #1 by Arne Ortlinghaus
X==nil was created by Arne Ortlinghaus
In our code I have seen many places where something is compared with nil.
Although it was never recommended it seems that it has worked in VO.
Is it better to rewrite it as Isnil(x) in X# or is the same code produced?
Arne

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago #2 by Chris Pyrgas
Replied by Chris Pyrgas on topic X==nil
Hi Arne,

The expression "x == NIL" is being translated to a call to the equals (==) operator method of the usual class, which uses much code to test against the possible combinations of values and types of the 2 values being compared.

On the other hand, IsNil() is a simple function call and this function is optimized to check against NIL only.

So it should be much faster to make the comparison against NIL with IsNil(), than using x == NIL. Of course for a few comparisons the difference in speed is not noticeable, but it might make a difference in tight time critical loops.

Chris

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago - 1 year 6 months ago #3 by Karl Faller
Replied by Karl Faller on topic X==nil
Chris,
pmfji, does that mean, we can use IsNil() with any type and get a correct answer ("not assigned"), as "Empty()" (should) have worked in VO? And is this available in all dialects?

Karl
Last edit: 1 year 6 months ago by Karl Faller. Reason: enhancement

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago #4 by Arne Ortlinghaus
Replied by Arne Ortlinghaus on topic X==nil
I would say:
Empty in X#/Vulcan Runtime gives the same result as Empty in VO
Isnil in X#/Vulcan Runtime gives the same result as Isnil in VO
If an empty string "" is passed then Empty returns true, whereas isnil return false
If a number 0 is passed then Empty returns true, whereas isnil return false
if a null_object then Empty returns true and also isnil return true
In X# with the Vulcan Runtime together with the macro execution I have had some cases where instead of a nil usual variable a null pointer was passed to function/method. Until now I have not understood in which cases this can happen.

I have seen that working with usuals there is always a big risk using isnil/==nil and I have preferred using Empty.

Arne

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago #5 by Chris Pyrgas
Replied by Chris Pyrgas on topic X==nil
Hi Karl,

What Arne said, IsNil() and Empty() check for different things.

Also IsNil() is a runtime function and its parameter type is USUAL, so you can use it only in the VO/Vulcan (or Harbour) dialects. And it is designed to work with USUAL vars, because the value NIL only makes sense in the context of the USUAL.

If you need a function that combines the Empty()/IsNil() functionality also in core, I would suggest to write a general function that behaves exactly the way you want it to, so you can use the same from all your apps.

Chris

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.