3
$\begingroup$

I want to make a Message from a List.

Message[LibraryFunction::argx, 1, 3]

LibraryFunction::argx: 1 called with 3 arguments; 1 argument is expected.

Apply[Message, Flatten[List[MessageName[LibraryFunction, "argx"], {2, 3}]]]

LibraryFunction::argx: 2 called with 3 arguments; 1 argument is expected.

After I define the MessageName it works fine with Message.

LibraryFunction::argx = "called with `1` argument(s) `2` argument(s) is expected."
Message[LibraryFunction::argx, 1, 3]

LibraryFunction::argx: called with 1 argument(s) 3 argument(s) is expected.

However with Apply it first deduces MessageName to string.

Apply[Message, Flatten[List[MessageName[LibraryFunction, "argx"], {2, 3}]]]

Message::name: Message name called with 1 argument(s) 2 argument(s) is expected. is not of the form symbol::name or symbol::name::language.

Message["called with `1` argument(s) `2` argument(s) is expected.", 2, 3]

What should I do such that the message is shown properly with Apply?

UPDATE

Kuba's answer works in general most of the time. However I have loaded a library with LibraryFunctionLoad

SomeFunctionWX = LibraryFunctionLoad["/path/to/libmod02.so", "SomeFunctionWX", LinkObject, LinkObject]
SomeFunctionWX::argx = "hallo `1` `2`"
Message[SomeFunctionWX::argx, 2, 3]

SomeFunctionWX::argx: hallo 2 3

Everything works if I use Message but when I use the Apply based method in the answer it breaks

Apply[Message[MessageName[#, #2], ##3] &, Flatten[{SomeFunctionWX, "argx", {2, 3}}]]

Message::name: Message name MessageName[LibraryFunction[/path/to/libmod02.so,SomeFunctionWX,LinkObject],argx] is not of the form symbol::name or symbol::name::language.

It evaluates SomeFunctionWX to LibraryFunction

UPDATE

The following worked.

Message[MessageName[SomeFunctionWX, "argx"], {2, 3} /. List -> Sequence]
$\endgroup$
  • $\begingroup$ In your edit, SomeFunctionWX evaluates to something else. Is there a reason why you want associate this symbol with a message? One could hack something together to make this work, but it doesn't really make sense to me. $\endgroup$ – jjc385 Oct 23 '18 at 18:47
  • $\begingroup$ As the function name is SomeFunctionWX it seems logical to see messages like SomeFunctionWX::rankerr $\endgroup$ – Neel Basu Oct 23 '18 at 18:56
  • $\begingroup$ Usually when we say the function's name is f, f by itself won't evaluate to anything. Perhaps it would make more sense for you to define something like SomeFunctionWX[args___] := LibraryFunctionLoad["/path/to/libmod02.so", "SomeFunctionWX", LinkObject, LinkObject][args]. You could even use a trick to make the LibraryFunctionLoad evaluate only once -- With[{lfl=LibraryFunctionLoad["/path/to/libmod02.so", "SomeFunctionWX", LinkObject, LinkObject]}, SomeFunctionWX[args___]:=lfl[args] ]. $\endgroup$ – jjc385 Oct 23 '18 at 19:04
  • $\begingroup$ But then the function name wouldn't evaluate before you try to apply Message/ MessageName to it. $\endgroup$ – jjc385 Oct 23 '18 at 19:05
4
$\begingroup$

Message is HoldFirst in order to handle MessageName well. You need to prevent MessageName from evaluation. It may be use case dependent but here is how I'd approach it:

Apply[
  Message[MessageName[#, #2], ##3] &,
  Flatten[{LibraryFunction, "argx", {2, 3}}]
]

This way you can manipulate name and arguments in the second line without worrying about MessageName evaluation etc.

$\endgroup$
  • $\begingroup$ I have loaded a library with SomeFunctionWX = LibraryFunctionLoad["libmod02.so", "SomeFunctionWX", LinkObject, LinkObject] then Message[SomeFunctionWX::argx, 2, 3] works but Apply[Message[MessageName[#, #2], ##3] &, Flatten[{SomeFunctionWX, "argx", {2, 3}}]] does not $\endgroup$ – Neel Basu Oct 23 '18 at 18:27
  • $\begingroup$ Please Check Edit $\endgroup$ – Neel Basu Oct 23 '18 at 18:33
  • $\begingroup$ @NeelBasu where do you get Flatten[{SomeFunctionWX, "argx", {2, 3}}] from? Could you give a little bit of context so I can address your use case well? $\endgroup$ – Kuba Oct 24 '18 at 6:15
  • $\begingroup$ I can send you the output in a few hours. I have the placeholder values in a List from there I am constructing the Message. I first tried to solve this using a List and then Apply. Actually this is for the library Mathematica++ that I am working on. Currently I have changed the storage from List to Sequence and things are working. But still I am curious in general how can I build it from list. In Mathematica++ I can append inside a sequence so this worked. However in general Append don't work on List. $\endgroup$ – Neel Basu Oct 24 '18 at 6:19
  • $\begingroup$ I think possibly if we change the head from list to sequence then it might work. $\endgroup$ – Neel Basu Oct 24 '18 at 6:23

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.