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" -> "[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.

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

1 Answer 1

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"

"[email protected]"

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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