10
$\begingroup$

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" -> "you@wolfram.com"|>, 
  "Sender" -> "me@wolfram.com", "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 "me@wolfram.com". 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.

$\endgroup$
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – Kuba Feb 16 at 15:12
7
$\begingroup$

It does not work because it was not implemented, imo by omission. What was implemented is an overloaded Part:

f[["File"]]

"MyFile.wl"

It won't work for Success though :)

What works for both of them is:

f[[2, "File"]]
s[[2, "Sender"]]

"MyFile.wl"

"me@wolfram.com"

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.