1. Testing for NULL
- Posted by xecronix Sep 02, 2015
- 2013 views
#!/usr/bin/env eui sequence x -- The next executable line is an error as expected. -- But, how can I test/defend against this error in advance? -- ie -- -- if x = ??? then -- -- do something -- end if printf(1, "x is %s", {x}) -- How to test for this error? -- No problem here. Works as I expected x = "some sequence" printf(1, "x is %s\n", {x}) -- I don't know how to express this in Euphoria either. -- What I hoped for was a way to set x to some value -- after valid assignment that would cause the error at the -- start of the code. x = 0 -- This causes a type error. But how do you unset a variable? printf(1, "x is %s\n", {x})
This came up when I wanted to do this
public function fname(sequence self, sequence x = 0) if x then self[FNAME] = x end if return self[FNAME] end function
2. Re: Testing for NULL
- Posted by SDPringle Sep 02, 2015
- 1994 views
The addition of testing EUPHORIA variables as being either not set or not is something that is relatively new. Some of us, like myself, say the user should have a set dummy value as values can be either atoms or sequences.
You can use object rather than sequence or use a custom type.
type sequence_or_0(object x) return equal(x,0) or sequence(x) end type sequence_or_0 x = 0 -- no fatal error here.
The variables should always be set to something. Here 0 means that there is no string yet.
public function fname(sequence_or_0 x) if compare(x,0) then self[FNAME] = x end if return self[FNAME] end function
Values that might not be atoms (numbers) must not be used or you may get a fatal error. If you use x=0, then you will get a sequence of 0s and 1s depending on how the individual characters compare to 0. You can use compare(), and equal(). These always return atoms. Only atoms are valid, non-zero atoms are true, and 0 is is false.
Shawn
3. Re: Testing for NULL
- Posted by jimcbrown (admin) Sep 02, 2015
- 2001 views
#!/usr/bin/env eui sequence x -- The next executable line is an error as expected. -- But, how can I test/defend against this error in advance? -- ie -- -- if x = ??? then -- -- do something -- end if printf(1, "x is %s", {x}) -- How to test for this error?
Only works in 4.0.0 and above - use object.
#!/usr/bin/env eui sequence x -- The next executable line is an error as expected. -- But, how can I test/defend against this error in advance? -- ie -- if not object(x) then -- do something end if printf(1, "x is %s", {x}) -- How to test for this error?
-- I don't know how to express this in Euphoria either. -- What I hoped for was a way to set x to some value -- after valid assignment that would cause the error at the -- start of the code. x = 0 -- This causes a type error. But how do you unset a variable? printf(1, "x is %s\n", {x})
This isn't supported.
4. Re: Testing for NULL
- Posted by petelomax Sep 02, 2015
- 1961 views
I could show you how to do this in Phix, using a grubby bit of inline assembly, which would need to be duplicated on a per variable basis. Oh, alright, you won't like it, but go on then:
-- WARNING: DO NOT USE, SIMPLY BE UTTERLY HORRIFIED! object x procedure is_x_assigned() ?object(x) end procedure is_x_assigned() x = "hello" -- note: after this point the compiler starts optimising away the "not assigned" handling for x... hence the use of is_x_assigned() is_x_assigned() #ilASM{ [32] mov edx,[x] mov [x],h4 cmp edx,h4 jle @f sub dword[ebx+edx*4-8],1 [64] mov rdx,[x] mov r15,h4 mov [x],r15 cmp rdx,r15 jle @f sub qword[rbx+rdx*8-16],1 [] jne @f call :%pDealloc @@: } is_x_assigned() -- output is: 0 1 0
However, it is obviously far easier just to make x always assigned to, say, {is_valid,actual_value}.
Pete
5. Re: Testing for NULL
- Posted by petelomax Sep 02, 2015
- 1993 views
Some of us, like myself, say the user should have a set dummy value as values can be either atoms or sequences.
When I read that I suddenly realised that a suitable dummy value could be 0.0 - ie:
if not integer(d) and d=0 then -- it /is/ the special dummy value
Of course you'd need some way to artificially create such a beast, and there are a couple of billion other "special values" that should not occur in normal code.
Pete
EDIT: Mind you, it could cause massive confusion and panic when things start being 0 and not 0 at the same time... And of course the d=0 as shown above would be optimized to false because the stupid compiler knows that d is not an integer, though you could use d=MYDUMMY instead. I'll get me coat.
6. Re: Testing for NULL
- Posted by andi49 Sep 02, 2015
- 1941 views
#!/usr/bin/env eui sequence x -- The next executable line is an error as expected. -- But, how can I test/defend against this error in advance? -- ie -- -- if x = ??? then -- -- do something -- end if printf(1, "x is %s", {x}) -- How to test for this error? -- No problem here. Works as I expected x = "some sequence" printf(1, "x is %s\n", {x}) -- I don't know how to express this in Euphoria either. -- What I hoped for was a way to set x to some value -- after valid assignment that would cause the error at the -- start of the code. x = 0 -- This causes a type error. But how do you unset a variable? printf(1, "x is %s\n", {x})
This came up when I wanted to do this
public function fname(sequence self, sequence x = 0) if x then self[FNAME] = x end if return self[FNAME] end function
Hi, i'am not really sure about what you are like todo.
But what about this? (Windows code)
-- #!/usr/bin/env eui include std/console.e -- for anykey() object x = 0 -- x="test" -- The next executable line is an error as expected. -- But, how can I test/defend against this error in advance? -- ie -- -- if x = ??? then -- -- do something -- end if if atom(x) then printf(1, "x is %d\n", {x}) -- How to test for this error? else printf(1, "x is %s\n", {x}) -- How to test for this error? end if -- No problem here. Works as I expected x = "some sequence" printf(1, "x is %s\n", {x}) -- I don't know how to express this in Euphoria either. -- What I hoped for was a way to set x to some value -- after valid assignment that would cause the error at the -- start of the code. x = 0 -- This causes a type error. But how do you unset a variable? -- printf(1, "x is %s\n", {x}) if atom(x) then printf(1, "x is %d\n", {x}) -- How to test for this error? else printf(1, "x is %s\n", {x}) -- How to test for this error? end if any_key()
7. Re: Testing for NULL
- Posted by SDPringle Sep 03, 2015
- 1912 views
x = 0 -- This causes a type error. But how do you unset a variable?
We ought to remove comments that are no longer valid after fixing problems. Leaving comments like this in can cause confusion. This line of code does not cause a type error like in the original post (by andi49).
x = 0 -- This no longer is an error because andi49 declared x as an "object".
That is better. I like the idea of sequence_or_0 type. Because you want to allow a NULL like value and a valid string but not any arbitrary numbers. It should be rather a string_or_0 type which allows strings and 0 and nothing else.
type string_or_0(object x) -- going into the type function x may be anything. if equal(0,x) then return 1 -- 0 ok elsif atom(x) then return 0 -- any other number not ok. else for i = 1 to length(x) do if not integer(x[i]) or 1 > x[i] or x[i] > #10FFFF then return 0 -- each member must be valid characters end if end for return 1 end if end type
Shawn
8. Re: Testing for NULL
- Posted by ghaberek (admin) Sep 04, 2015
- 1906 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
9. Re: Testing for NULL
- Posted by SDPringle Sep 04, 2015
- 1810 views
public type String( object x ) return string( x ) or equal( x, NULL ) end type
-Greg
I think there is value in having a distinct type that allow for the dummy value. Naming convention preferences aside I would call it StringOrNull but not String. I would make another type String that would NOT allow NULL.
public type String( object x ) return string(x) end type
10. Re: Testing for NULL
- Posted by ghaberek (admin) Sep 04, 2015
- 1864 views
- Last edited Sep 05, 2015
I think there is value in having a distinct type that allow for the dummy value. Naming convention preferences aside I would call it StringOrNull but not String. I would make another type String that would NOT allow NULL.
Yeah, that's probably a better idea. C# allows for this with Nullable Types, so "string?" and "string" are two different things (nullable and non-nullable, respectively).
Too bad "?" isn't a valid type name character in Euphoria.
public type String( object x ) return string(x) end type public type StringOrNull( object x ) return string(x) or equal(x, NULL) end type
-Greg
P.S. I'm not trying to compare Euphoria to C#, just using it as an example to show that such conventions are already established in other languages.