What is the difference between? [missing value]

What is the difference between a missing value and an empty value?

To be specific,


on IsMissing(arg)
	
	(*
	-- thanks to http://bbs.applescript.net/
	try
		arg
		return false
	on error
		return true
	end try
	*)
	
	return (arg is missing value)
	
end IsMissing


on IsEmpty(arg)
	
	set argStr to arg as string
	
	if (length of argStr > 0) then
		return false
	else
		return true
	end if
	
end IsEmpty

What I am considering is this function:


on ArgumentsBlank(argList)
	
	set result to false
	
	set theArgs to argList as list
	
	if (length of theArgs = 0) then
		set result to true
	else
		repeat with i from 1 to count of theArgs
			
			set arg to item i of theArgs
			
			if (my IsMissing(arg) or my IsEmpty(arg)) then
				set result to true
				exit repeat
			end if
			
		end repeat
	end if
	
	return result
	
end ArgumentsBlank

which call looks like:


ArgumentsBlank(arg1, arg2, arg3, ...)

for example:


ArgumentsBlank(arg1, , arg3, "")

I want to be able to pass an empty string=“” as a valid parm, but otherwise, pass null as is represented above by the missing arg2.

I thought I understood all this back when, but now I’m getting very confused very quickly.

Thanks in advance,

John Love

Hi John,

¢ missing value is no value at all, you get an error with the count or length command
¢ empty value is e.g an empty string “” or an empty list {}.

But your script doesn’t work properly, because you’re using a reserved variable (result) as a user variable.

You can set result, but you shouldn’t use that because as Stefan said it’s reserved to hold the last result of a command.

You cannot pass no value as a parameter, but you could pass a list with missing items. e.g.


set l to {1, 2, 3}
set end of l to NoValue()
set item 2 of l to NoValue()
set end of l to 4
DoSomething(l)
--
on NoValue()
	return
end NoValue
--
on DoSomething(x)
	return x
end DoSomething

gl,

I added some dialogs:


set l to {1, 2, 3}
set end of l to RemoveValue()
set item 2 of l to RemoveValue()
set end of l to 4
DoSomething(l)
--
on RemoveValue()
	return
end RemoveValue
--
on DoSomething(x)
	set c to count x
	repeat with i from 1 to c
		try
			set y to item i of x
			display dialog y
		on error
			display dialog "no value"
		end try
	end repeat
	return
end DoSomething

gl,

‘missing value’ is an object. I’m not quite sure what you mean by ‘empty value’ - it is possible in AppleScript to declare a variable without assigning a value to it; e.g.:

on foo()
    return
end foo

set var to foo()
get var -- this errors as foo didn't return a value

Perhaps that’s what you’re thinking of? It’s basically a language wart (AppleScript has a lot of those, unfortunately) and something you want to avoid if at all possible.

That won’t work - in the event that you call IsMissing without a parameter, AppleScript will just raise a ‘parameter is missing’ error and the handler code will never be invoked.

AppleScript doesn’t support variable length argument lists. You mean:


ArgumentsBlank({arg1, arg2, arg3, ...})

That is syntactically invalid and won’t compile.

If the problem is that you’re dealing with one of those badly designed applications that don’t always return value when a value is expected - e.g. older versions of Address Book were known to do this when getting properties which hadn’t been filled in - there are usually workarounds for dealing with that, so post specific details if that’s the issue. But as far as your own code goes, use the ‘missing value’ constant when you need an object which effectively means ‘no value’ but still behaves as a normal object, e.g.:


ArgumentsBlank({arg1, missing value, arg3, ""})

HTH

has

Hi hhas,

of course it works, try this:

IsMissing(1) -- false
IsMissing(missing value) -- true

on IsMissing(arg)
	return (arg is missing value)
end IsMissing

hhas was talking about something like this:

IsMissing()
--> Error -1721: "{} doesn't match the parameters {arg} for IsMissing."

on IsMissing(arg)
	-- whatever
end IsMissing

I know, Bruce, but this never happens.
If you have a handler call with a variable, always “something” will be passed,
and even missing value is something

There’s some confusion here, owing to the fact that the original poster’s asked the difference between one term he’s used ambiguously and another he’s invented entirely. :slight_smile:

Apologies for the mixup - there’s something about AppleScript that makes confusion contagious. I misread the original code and didn’t notice that the try block - which is clearly intended to detect unassigned variables - was disabled. Please just ignore that part of my post.

Thanks for the use of a keyword = result as a parm, which is a “no-no”. Therefore I have re-written this:


on ArgumentsBlank(argList)
	
	if (IsMissing(argList)) then return true
	
	set blank to false
	
	set theArgs to argList as list
	
	repeat with i from 1 to count of theArgs
		
		set arg to item i of theArgs
		
		(*
			OR is a "short-circuit" boolean, so,
			if missing, the test for empty doesn't occur.
			(good thing, since IsEmpty doesn't understand missing)
		*)
		if (my IsMissing(arg) or my IsEmpty(arg)) then
			set blank to true
			exit repeat
		end if
		
	end repeat
	
	return blank
	
end ArgumentsBlank

Also, thanks for the beautiful synonym for “empty value”, that is “language wart” … am going to add the latter to my vocabulary … perfectly descriptive … the closest example that I can think of is way, way, way back when, “ain’t” was invented and soon (unfortunately) became commonly used, that is to say, became a “language wart”.

Back to AppleScript …

To me, “missing” means effectively = null, that is, not even defined; for example, in ArgumentsBlank(arg1, , arg3, “”) arg2 is null, It doesn’t exist. However, (perhaps only for me and others easily confused) it does exist and its value = null – I think the character equivalent is “\0” ??

Is the above statement correct?

Next, the “language wart”, that is “empty value”. By this I mean the parm, arg, was created or declared, but a value was never assigned to it, that is, never instantiated.

Missing:
What I am really after is to be able to check for accidentally calling:
ArgumentsBlank(arg1, , arg3, “”)
which has a missing arg2, that is, it is just not there at all.

Empty:
Also, I want to check for:
ArgumentsBlank(arg1, arg2, arg3, “”)
which has arg2, but it has no assigned value.

On my initial submission I had:


on IsMissing(arg)
   
   (*
   -- thanks to http://bbs.applescript.net/
   try
       arg
       return false
   on error
       return true
   end try
   *)
   
   return (arg is missing value)
   
end IsMissing

Just one question on this script - is the commented-out try loop functionally identical to (arg is missing value)?

Next, I’ve redone this:


on IsEmpty(arg)
	
	if (class of arg is string) then
		return false -- allows arg = ""
	else
		return (length of (arg as string) = 0)
	end if
	
end IsEmpty

It seems the “else” case addresses a defined arg, but with no assigned value at all???

Next, hhas responded that “AppleScript doesn’t support variable length argument lists.” and asked if I meant:

ArgumentsBlank({arg1, arg2, arg3, …})

No, what I tried to mean was to check for accidentally calling:
ArgumentsBlank(arg1, , arg3)
where arg2 is just not there or, if “there”, has a value of null. Anyway, this is the call I want to check for.

Before I totally lose it, I will always call:

if (IsMissing(arg) or IsEmpty(arg)) then etc

to check for missing first since IsEmpty right now does not understand “missing”.

I cannot thank you enough for continuing with this saga.

John Love

“If I am not all there, does that mean that I am here?”

No, in ArgumentsBlank(arg1, , arg3, “”), the second argument is simply missing, which means the code won’t compile.

Okay, the full and sordid story FYI:

Firstly, ‘missing value’ is an ordinary object of type ‘class’; its raw form is «class msng». It’s not literally the same thing as ‘undefined’, although it is the value used to indicate ‘no other value’, as it were.

Secondly, AppleScript does also define a ‘null’ object of type ‘class’; its raw form is «class null». This is also an ordinary object; however, it isn’t something you’ll encounter unless you use it yourself in your own code. And since ‘missing value’ is the standard value to use for indicating ‘no other value’, it’s not something you should be using. Therefore, you should completely ignore it.

Finally, AppleScript does in fact use a third type of object to represent an ‘undefined’ state; this is a value of type ‘null’. It has no raw form in AppleScript (i.e. it’s invisible), since it’s purely an implementation detail and not something that’s meant to be visible to users. And it’s not something you need to concern yourself with, except in the very rare situations where AppleScript’s notoriously buggy nature inadvertently exposes it to you - either because you were doing something that was ill-advised in the first place (in which case, don’t do that) or because you were dealing with a badly designed application (in which case, file a bug report on it so the problem gets fixed at source).

The ASCII 0 character is otherwise known as NUL, but is no relation to ‘missing value’. In some languages (e.g. C) ASCII 0 has special significance, but not in AppleScript where it’s just a character like any other.

To repeat: this code is syntactically invalid. It won’t compile; therefore it can’t exist in a working script.

If the variable ‘arg2’ has no value (which due to AppleScript’s flawed design is occasionally possible), AppleScript will raise an error as soon as it tries to execute that command. So there’s no point in the ‘ArgumentsBlank’ handler trying to check for that condition, as it’ll never get that far.

No. Also, see above. Even if you uncomment the ‘try’ block, the ‘on error…end’ portion will never actually be executed as it’s impossible for AppleScript to get that far unless the caller supplied an argument to the ‘IsMissing’ command.

See above. This code is invalid. It’s impossible to execute it, accidentally or otherwise. If you try to compile:

ArgumentsBlank(arg1, , arg3)

AppleScript will raise a compilation error.

If you try to execute:

set arg1 to 1
set arg3 to 3
ArgumentsBlank(arg1, arg2, arg3)

AppleScript will raise an ‘undefined variable’ error on the last line. If you try to execute:

on foo()
end foo

set arg1 to 1
set arg2 to foo()
set arg3 to 3
ArgumentsBlank(arg1, arg2, arg3)

AppleScript will still raise an ‘undefined variable’ error on the last line. Therefore, this is not a condition that the ‘ArgumentsBlank’ handler has to check for, as if it does occur the script will fail before the handler is ever executed.

Now, due to the defects in AppleScript’s design, it is possible to find situations where you encounter a ‘non-value value’, e.g.:

on foo()
end foo

set lst to {1, 2}
set end of lst to foo()
set end of lst to foo()
lst
--> {1, 2,,}

The above is an example of self-inflicted stupidity: if callers expect handler ‘foo’ to return a value then handler ‘foo’ should always return a value, even if that value is only ‘missing value’. In other words, fix the code so this situation never arises.

Offhand, I can’t think of an extant example of application-inflicted stupidity, although as I’ve said, older versions of Address Book were notorious for not always returning values when values were expected, and you had to do stuff like (e.g.):

set maidenName to maiden name of person [i]as string[/i]

to force AppleScript to assign a proper value to variable ‘maidenName’ instead of ‘undefined’ (coercing type ‘null’ to string gives “”, which is what Address Book should’ve been returning in the first place; that or ‘missing value’).

Oh, and there was an amusing bug in my own TextCommands application where the ‘check’ command forgot to return a value in certain situations, but that’s fixed now.

So anyway, this stuff is confusing, but only because 1. AppleScript is badly designed and buggy, and/or 2. you were doing things that you really should avoid (since they encourage said bugs and design defects to manifest). For your purposes, the only thing you should have to worry about is ‘missing value’ (unless you’re getting faulty data from a badly designed application, in which case, post a specific example so you can be advised on the exact workaround to use).

Man. It’s stuff like this that reminds me why I’ve largely bailed for Python and Ruby these days. The syntax may not be as superficially pretty, but at least the semantics aren’t a raging mess of psychoses like some we could mention… :stuck_out_tongue:

(Or, as Nietzche put it: If you stare into the Abyss long enough the Abyss stares back at you.)

Completely OT comment re Nietzche:

A fact chillingly noticed almost immediately by anyone who goes skin diving or snorkeling in very deep water (in my case cleaning off the short-wave radio grounding plate of a sailboat I was delivering to Bermuda from Boston). After only a few seconds, you become convinced that although you can’t see anything straight down, something evil down there can see you clearly. Spooky. Odd that Nietzche should predict that because I’ll bet he never went snorkeling in the gulf stream.

PS: one of the guys on my crew brought a sailing surfboard on the trip so he could sail it while we were 700 miles from anywhere. He said he could hear whales, but didn’t see any.

Hi John,

Here’s another example using iCal and a newly created event where the ‘description’ property always returns no value.


tell application "iCal"
	set c to first calendar whose title is "Home"
	set e to first event of c
	tell e
		set l to {summary, start date, end date, description}
	end tell
end tell
CheckValues(l)

on CheckValues(l)
	set {arg1, arg2, arg3, arg4} to l
	try
		arg1
		arg2
		arg3
		arg4
		set blank to false
	on error
		set blank to true
	end try
	return blank
end CheckValues

gl,

Yeah, that’s the sort of badly-designed-application bug I was talking about earlier, where the ‘get’ command should return “” or ‘missing value’ if the event hasn’t been given a description. Still broken in Tiger, I notice; might want to file a bug report on that.

In this case, since getting the description property is expected to return Unicode text, the best and simplest workaround is to insert a coercion like this:

		set l to {summary, start date, end date, description as Unicode text}

This will force AppleScript to return an empty string if the description is missing, instead of horking ugly internal implementation details all over your nice clean code.

Nice solution has. I didn’t know you could corce undefined to empty string.

Edited: just thought of something that might not fit into this solution. suppose you found a bad app that returned undefined for a property that retturns a date class. You might not want to coerce this to string, but it works perfectly well in iCal as as the only properties I can see, that returns undefined, returns strings.

gl,

Just a million applause claps for your extraordinary patience … there’s a magical IsMissing as well as a IsBlank function within VBA that do what I wish; however, your learned responses say not duplicated by AppleScript.

A million “thank you’s”.