I'm trying to figure out what is a quick way to add error reporting to my user defined functions. I was experimenting with Success
and Failure
until I stumbled upon what seems to me an oddity of sorts. The examples are taken from the Documentation Center:
f = Failure["ExternalOperation", <|
"MessageTemplate" -> "External operation `1` failed.",
"MessageParameters" -> {"file upload"},
"File" -> "MyFile.wl"|>];
and
s = Success["MailSent", <|
"MessageTemplate" :> "Mail sent to `recipient`.",
"MessageParameters" -> <|"recipient" -> "[email protected]"|>,
"Sender" -> "[email protected]", "Recipient" -> "you@wolfram.com",
"MessageID" -> "4ac4dsdasd483k28d4"|>];
In my code I found really useful the fact that it was possible to use something like s["Sender"]
and obtain the desired result "[email protected]"
. That way, you could pass results and other information inside the Success
wrapper.
Now, the odd thing is that such is not the case with Failure
. Trying to evaluate f["File"]
will not return "MyFile.wl"
as I'd expect, but will return the Failure
wrapper with the supplied key, like in Failure[...]["File"]
.
I understand it is possible to obtain the desired result using something like Extract[f, {2, Key["File"]}]
but I'd rather use the f["File"]
approach, if that were possible.
My question is, why do Failure
and Success
behave differently despite the fact they appear to implement the same underlying interface in their definitions?
If I am wrong to anticipate that they should behave similarly, what am I missing? Is there something inherent in the way Failure
is implemented that makes it different?
-- Edit --
It appears that it's no longer an issue in v.12. That's very thoughtful of them.