Re: Testing for NULL
- Posted by ghaberek (admin) Sep 04, 2015
- 1905 views
I propose naming these new types with CamelCase, vis-a-vis the string type and String class in C#.
IMHO, this looks cleaner than adding "_or_0" to the type name.
It would also be helpful to have "is null or empty" functions for these new types.
include std/dll.e -- includes NULL include std/types.e -- includes string type -- -- returns 1 if x is NULL -- public function IsNull( object x ) return equal( x, NULL ) end function -- -- returns 1 if length(x) = 0 -- public function IsEmpty( object x ) return sequence( x ) and length( x ) = 0 end function -- -- returns 1 if a x is NULL or length(x) = 0 -- public function IsNullOrEmpty( object x ) return IsNull( x ) or IsEmpty( x ) end function -- -- a sequence type that allows NULL -- public type Sequence( object x ) return sequence( x ) or equal( x, NULL ) end type -- -- a string type that allows NULL -- public type String( object x ) return string( x ) or equal( x, NULL ) end type
Example:
String myName = NULL -- valid if IsNullOrEmpty( myName ) then myName = "John Smith" -- valid end if myName = 3.14159 -- invalid
-Greg