Category Archives: Pointers

Pointer validation expression

When handling pointers (or more general ‘Objects’ in Java) you often have to check or rather validate your pointers to not be a null-pointer.

In C++ for instance you could have such a code:

// Function creates an entity and returns it as a pointer
Entity* CreateEntity() { /* ... */ }

void DoSomethingWithFirstEntity(Entity** listOfEntities)
{
	// Make sure we don't have null pointers
	if (listOfEntities != nullptr && *listOfEntities != nullptr)
	{
		(**listOfEntities).DoSomething();
	}
}

Of course, C++ programmers should use references or const-references as much as possible,
so you don’t have to do this pointer validation.
But in some cases you still need to do such things.

In XieXie this is again much more comfortable:

Entity* CreateEntity() { /* ... */ }

void DoSomethingWithFirstEntity(Entity** listOfEntities) {
	if valid listOfEntities {
		listOfEntities.DoSomething()
	}
}

This XieXie code will translate exactly to the above C++ code.
No matter how many pointers must be dereferenced, XieXie does it for you ;-)

Here is another example:

// XieXie code:
int a
int* b
int**@ c

/* ... */

bool b1 := valid a
bool b2 := valid b
bool b3 := valid c

This will translate to:

// C++ code:
int a = 0;
int* b = 0;
std::shared_ptr< int** > c = nullptr;

/* ... */

bool b1 = true;
bool b2 = (b != nullptr);
bool b3 = (c != nullptr && *c != nullptr && **c != nullptr);