Node: Returning C++ type names, Next: , Previous: Naming Conventions, Up: Name Space Issues



Returning C++ type names

In some cases in Lpp where it might be useful and more efficient to return a primitive C++ type, such as an int, the Common Lisp borrowed function name is used, since it is expected in C++ programs that that name will be used more frequently. However a counterpart function is still needed to return the corresponding Lpp object, such as an Lpp Integer object. Such counterpart functions can also be coerced to a function object for use in funcall and apply. In these cases if the Lpp function that returns the primitive C++ type is named

     function
     

the the counterpart that returns the corresponding Lpp type is named

     functionL
     

The capital L is meant to connote that an Lpp object is the result. For example the Common Lisp borrowed name length that returns the length of a sequence is used in Lpp to return an int instead of an Lpp Integer object

     int i = length(list);
     

and the counterpart function name is lengthL

     let i = lengthL(list);
     let i = funcall(L(lengthL), list);
     

The length function is more efficient and does not generate an Lpp Integer object. Whereas the lengthL does generate an Lpp Integer object if needed and as seen above can also be used as a function object whereas the primitive length can not, See Functions.

This is an unfortunate situation, since it would be better to have the function, such as length, be overloaded on both the int returning function and the let returning function. But some C++ compilers issue an ambiguous function name error in this case.

For efficiency concerns this is consistently done in all cases where the Common Lisp borrowed name of a function returns an integer. In such cases where the Common Lisp function would return either a integer or nil then a -1 is returned for the int returning version. For example the Common Lisp function list-length returns the length of a given list as an interger but returns nil if the list is circular

     int i = listLength(list);        // i == -1 if list is circular
     let i = listLengthL(list);       // i == nil if list is circular
     

We mention the unfortunate situation above, but from another perspective the functionL function name comments the fact that an Lpp object will be generated on the heap. To illustrate this, following is bad

     if (listLengthL(list)) // then do something
     

Although it would work, the listLengthL returned value is not referenced anywhere and hence would cause a memory leak. It would be better done with

     let len = listLengthL(list);
     if (len) // then do something
     

or if the Lpp Integer object is not needed

     if (listLength(list) > -1) // then do something