<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body>
<div dir="auto" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
Julien: thank you for this. Your mercury-java library has been a key resource.</div>
<div dir="auto" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
<br>
</div>
<div dir="auto" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
I have submitted a pull request (https://github.com/Mercury-Language/mercury/pull/139) that adds generic types to the "extends" clause, which addresses mve.m. This means that we can write Java code to catch exceptions and write them to Mercury types (from Java).</div>
<div dir="auto" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
<br>
</div>
<div dir="auto" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
By my understanding, we would need to add generic types for "new" Mercury types which have generic types (e.g. as per mve2.m and mve3.m). This is not addressed by the submitted pull request.</div>
<div dir="auto" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
<br>
</div>
<div dir="auto" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
---- Mark</div>
<div id="ms-outlook-mobile-body-separator-line" data-applydefaultfontstyles="true" style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir="auto">
<br>
</div>
<div style="font-family:Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif;font-size:12pt" dir="auto" id="ms-outlook-mobile-signature">
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Julien Fischer <jfischer@opturion.com><br>
<b>Sent:</b> Friday, September 19, 2025 3:27:19 PM<br>
<b>To:</b> Mark Clements <mark.clements@ki.se><br>
<b>Cc:</b> users@lists.mercurylang.org <users@lists.mercurylang.org><br>
<b>Subject:</b> Re: [m-users.] Java exceptions and nested classes</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">[You don't often get email from jfischer@opturion.com. Learn why this is important at
<a href="https://aka.ms/LearnAboutSenderIdentification">https://aka.ms/LearnAboutSenderIdentification</a> ]<br>
<br>
Hi Mark,<br>
<br>
On Fri, 19 Sept 2025 at 21:05, Mark Clements <mark.clements@ki.se> wrote:<br>
><br>
> I am investigating using JDBC with Mercury. Most of the JDBC classes throw an SQLException, which I want to catch in Java and return a result type with either an ok() value or an error(). I have the following minimal example (not using JDBC):<br>
><br>
> :- module mve.<br>
> :- interface.<br>
> :- import_module io, int.<br>
> :- type result(T,E) ---> ok(T) ; error(E).<br>
> :- type exception.<br>
> :- pred some_pred(int::in, mve.result(int,exception)::out, io::di, io::uo) is det.<br>
> :- pred main(io::di, io::uo) is det.<br>
> :- implementation.<br>
> :- pragma foreign_type("Java", exception, "java.lang.Exception").<br>
> :- pragma foreign_proc("Java",<br>
> some_pred(A::in, C::out, _IO0::di, _IO1::uo),<br>
> [will_not_call_mercury, thread_safe, promise_pure],<br>
> "try {<br>
> C = new Result_2.Ok_1<Integer,java.lang.Exception>(A);<br>
> } catch(java.lang.Exception e) {<br>
> System.err.println(e.getMessage());<br>
> C = new Result_2.Error_1<Integer,java.lang.Exception>(e);<br>
> }").<br>
> main(!IO) :-<br>
> some_pred(1, C, !IO),<br>
> (C = ok(_) ; C = error(_)).<br>
><br>
> However, when I compile this I get warnings, such as:<br>
><br>
> mve.m:15: warning: [unchecked] unchecked conversion<br>
> C = new Result_2.Ok_1<Integer,java.lang.Exception>(A);<br>
> ^<br>
> required: Result_2<Integer,Exception><br>
> found: Ok_1<Integer,Exception><br>
<br>
Under the current compilation scheme for Java, you are not going to be able<br>
to avoid those warnings. Mercury's use of generics in the Java grades is<br>
rather limited. (Much of the generated code effectively works with raw types.)<br>
<br>
> Alternatively, is there a better way to catch the Java exceptions?<br>
<br>
I assume you mean: is there a better way to pass them back to Mercury?<br>
(Obviously, the only way to catch them is to use the exception<br>
handling mechanisms<br>
that Java provides.)<br>
<br>
I would avoid relying on how Mercury represents types in Java and either use<br>
exported functions to build the result terms (as per the attached<br>
mve2.m example)<br>
or just return a flag from the Java code indicating what has happened and deal<br>
with constructing result terms in Mercury (as per the attached mve3.m example).<br>
<br>
(Some time ago I wrote <<a href="https://github.com/juliensf/mercury-java">https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjuliensf%2Fmercury-java&data=05%7C02%7Cmark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938853514514300%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=67vstV8DKDJ%2BjI3onCJyWrbb3EoVUTvp2XgbhWtOekI%3D&reserved=0</a>>,
which not<br>
covering JDBC, may give you some idea for how to approach interfacing with<br>
Java APIs.)<br>
<br>
Julien.<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
><br>
> --- Mark.<br>
><br>
><br>
> När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter. Här finns information om hur KI behandlar personuppgifter<<a href="https://ki.se/om-ki/integritetsskyddspolicy">https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fki.se%2Fom-ki%2Fintegritetsskyddspolicy&data=05%7C02%7Cmark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938853514544291%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=lntdNO1jOwSqG1ZPp8lNisKASWQ%2BsNOA5UPfHJ9w8oA%3D&reserved=0</a>>.<br>
><br>
><br>
> Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI’s processing of personal data here<<a href="https://staff.ki.se/data-protection-policy">https://staff.ki.se/data-protection-policy</a>>.<br>
> _______________________________________________<br>
> users mailing list<br>
> users@lists.mercurylang.org<br>
> <a href="https://lists.mercurylang.org/listinfo/users">https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.mercurylang.org%2Flistinfo%2Fusers&data=05%7C02%7Cmark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938853514565787%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=lSTPQxf8lrNqHNC%2B%2BeSgu8o96ZR81THpbOogzCv3BCM%3D&reserved=0</a><br>
</div>
</span></font></div>
</body>
</html>