Re: [m-users.] Java exceptions and nested classes

Mark Clements <[email protected]> Mon, 22 Sep 2025 04:26:52 +0000
Newsgroups gmane.comp.lang.mercury.general
Message-ID <GV3P280MB00011532E99E494471DC785CF012A@GV3P280MB0001.SWEP280.PROD.OUTLOOK.COM>
--===============0572337526355974738==
Content-Language: en-GB
Content-Type: multipart/alternative;
	boundary="_000_GV3P280MB00011532E99E494471DC785CF012AGV3P280MB0001SWEP_"

--_000_GV3P280MB00011532E99E494471DC785CF012AGV3P280MB0001SWEP_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Julien: thank you for this. Your mercury-java library has been a key resour=
ce.

I have submitted a pull request (https://github.com/Mercury-Language/mercur=
y/pull/139) that adds generic types to the "extends" clause, which addresse=
s mve.m. This means that we can write Java code to catch exceptions and wri=
te them to Mercury types (from Java).

By my understanding, we would need to add generic types for "new" Mercury t=
ypes which have generic types (e.g. as per mve2.m and mve3.m). This is not =
addressed by the submitted pull request.

---- Mark

________________________________
From: Julien Fischer <[email protected]>
Sent: Friday, September 19, 2025 3:27:19 PM
To: Mark Clements <[email protected]>
Cc: [email protected] <[email protected]>
Subject: Re: [m-users.] Java exceptions and nested classes

[You don't often get email from [email protected]. Learn why this is im=
portant at https://aka.ms/LearnAboutSenderIdentification ]

Hi Mark,

On Fri, 19 Sept 2025 at 21:05, Mark Clements <[email protected]> wrote:
>
> I am investigating using JDBC with Mercury. Most of the JDBC classes thro=
w an SQLException, which I want to catch in Java and return a result type w=
ith either an ok() value or an error(). I have the following minimal exampl=
e (not using JDBC):
>
> :- module mve.
> :- interface.
> :- import_module io, int.
> :- type result(T,E) ---> ok(T) ; error(E).
> :- type exception.
> :- pred some_pred(int::in, mve.result(int,exception)::out, io::di, io::uo=
) is det.
> :- pred main(io::di, io::uo) is det.
> :- implementation.
> :- pragma foreign_type("Java", exception, "java.lang.Exception").
> :- pragma foreign_proc("Java",
>     some_pred(A::in, C::out, _IO0::di, _IO1::uo),
>     [will_not_call_mercury, thread_safe, promise_pure],
>     "try {
>          C =3D new Result_2.Ok_1<Integer,java.lang.Exception>(A);
>     } catch(java.lang.Exception e) {
>          System.err.println(e.getMessage());
>          C =3D new Result_2.Error_1<Integer,java.lang.Exception>(e);
>     }").
> main(!IO) :-
>     some_pred(1, C, !IO),
>     (C =3D ok(_) ; C =3D error(_)).
>
> However, when I compile this I get warnings, such as:
>
> mve.m:15: warning: [unchecked] unchecked conversion
>          C =3D new Result_2.Ok_1<Integer,java.lang.Exception>(A);
>              ^
>   required: Result_2<Integer,Exception>
>   found:    Ok_1<Integer,Exception>

Under the current compilation scheme for Java, you are not going to be able
to avoid those warnings.  Mercury's use of generics in the Java grades is
rather limited.  (Much of the generated code effectively works with raw typ=
es.)

> Alternatively, is there a better way to catch the Java exceptions?

I assume you mean: is there a better way to pass them back to Mercury?
(Obviously, the only way to catch them is to use the exception
handling mechanisms
that Java provides.)

I would avoid relying on how Mercury represents types in Java and either us=
e
exported functions to build the result terms (as per the attached
mve2.m example)
or just return a flag from the Java code indicating what has happened and d=
eal
with constructing result terms in Mercury (as per the attached mve3.m examp=
le).

(Some time ago I wrote <https://eur01.safelinks.protection.outlook.com/?url=
=3Dhttps%3A%2F%2Fgithub.com%2Fjuliensf%2Fmercury-java&data=3D05%7C02%7Cmark=
.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3d=
a1dda043c05d%7C0%7C0%7C638938853514514300%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0e=
U1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoy=
fQ%3D%3D%7C60000%7C%7C%7C&sdata=3D67vstV8DKDJ%2BjI3onCJyWrbb3EoVUTvp2XgbhWt=
OekI%3D&reserved=3D0<https://github.com/juliensf/mercury-java>>, which not
covering JDBC, may give you some idea for how to approach interfacing with
Java APIs.)

Julien.








>
> --- Mark.
>
>
> N=E4r du skickar e-post till Karolinska Institutet (KI) inneb=E4r detta a=
tt KI kommer att behandla dina personuppgifter. H=E4r finns information om =
hur KI behandlar personuppgifter<https://eur01.safelinks.protection.outlook=
.com/?url=3Dhttps%3A%2F%2Fki.se%2Fom-ki%2Fintegritetsskyddspolicy&data=3D05=
%7C02%7Cmark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1=
cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938853514544291%7CUnknown%7CTWFpbGZsb=
3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFp=
bCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=3DlntdNO1jOwSqG1ZPp8lNisKASWQ%2=
BsNOA5UPfHJ9w8oA%3D&reserved=3D0<https://ki.se/om-ki/integritetsskyddspolic=
y>>.
>
>
> Sending email to Karolinska Institutet (KI) will result in KI processing =
your personal data. You can read more about KI=92s processing of personal d=
ata here<https://staff.ki.se/data-protection-policy>.
> _______________________________________________
> users mailing list
> [email protected]
> https://eur01.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F%2Flists=
.mercurylang.org%2Flistinfo%2Fusers&data=3D05%7C02%7Cmark.clements%40ki.se%=
7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7=
C0%7C638938853514565787%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlY=
iOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7=
C%7C%7C&sdata=3DlSTPQxf8lrNqHNC%2B%2BeSgu8o96ZR81THpbOogzCv3BCM%3D&reserved=
=3D0<https://lists.mercurylang.org/listinfo/users>

--_000_GV3P280MB00011532E99E494471DC785CF012AGV3P280MB0001SWEP_
Content-Type: text/html; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DWindows-1=
252">
</head>
<body>
<div dir=3D"auto" style=3D"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 resour=
ce.</div>
<div dir=3D"auto" style=3D"font-family: Aptos, Aptos_MSFontService, -apple-=
system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
<br>
</div>
<div dir=3D"auto" style=3D"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/mercur=
y/pull/139) that adds generic types to the &quot;extends&quot; clause, whic=
h addresses mve.m. This means that we can write Java code to catch exceptio=
ns and write them to Mercury types (from Java).</div>
<div dir=3D"auto" style=3D"font-family: Aptos, Aptos_MSFontService, -apple-=
system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
<br>
</div>
<div dir=3D"auto" style=3D"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 &quot;new&quot;=
 Mercury types which have generic types (e.g. as per mve2.m and mve3.m). Th=
is is not addressed by the submitted pull request.</div>
<div dir=3D"auto" style=3D"font-family: Aptos, Aptos_MSFontService, -apple-=
system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
<br>
</div>
<div dir=3D"auto" style=3D"font-family: Aptos, Aptos_MSFontService, -apple-=
system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;">
---- Mark</div>
<div id=3D"ms-outlook-mobile-body-separator-line" data-applydefaultfontstyl=
es=3D"true" style=3D"font-family: Aptos, Aptos_MSFontService, -apple-system=
, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir=3D"auto">
<br>
</div>
<div style=3D"font-family:Aptos, Aptos_MSFontService, -apple-system, Roboto=
, Arial, Helvetica, sans-serif;font-size:12pt" dir=3D"auto" id=3D"ms-outloo=
k-mobile-signature">
</div>
<hr style=3D"display:inline-block;width:98%" tabindex=3D"-1">
<div id=3D"divRplyFwdMsg" dir=3D"ltr"><font face=3D"Calibri, sans-serif" st=
yle=3D"font-size:11pt" color=3D"#000000"><b>From:</b> Julien Fischer &lt;jf=
[email protected]&gt;<br>
<b>Sent:</b> Friday, September 19, 2025 3:27:19 PM<br>
<b>To:</b> Mark Clements &lt;[email protected]&gt;<br>
<b>Cc:</b> [email protected] &lt;[email protected]&gt;<=
br>
<b>Subject:</b> Re: [m-users.] Java exceptions and nested classes</font>
<div>&nbsp;</div>
</div>
<div class=3D"BodyFragment"><font size=3D"2"><span style=3D"font-size:11pt;=
">
<div class=3D"PlainText">[You don't often get email from jfischer@opturion.=
com. Learn why this is important at
<a href=3D"https://aka.ms/LearnAboutSenderIdentification">https://aka.ms/Le=
arnAboutSenderIdentification</a> ]<br>
<br>
Hi Mark,<br>
<br>
On Fri, 19 Sept 2025 at 21:05, Mark Clements &lt;[email protected]&gt; wr=
ote:<br>
&gt;<br>
&gt; I am investigating using JDBC with Mercury. Most of the JDBC classes t=
hrow an SQLException, which I want to catch in Java and return a result typ=
e with either an ok() value or an error(). I have the following minimal exa=
mple (not using JDBC):<br>
&gt;<br>
&gt; :- module mve.<br>
&gt; :- interface.<br>
&gt; :- import_module io, int.<br>
&gt; :- type result(T,E) ---&gt; ok(T) ; error(E).<br>
&gt; :- type exception.<br>
&gt; :- pred some_pred(int::in, mve.result(int,exception)::out, io::di, io:=
:uo) is det.<br>
&gt; :- pred main(io::di, io::uo) is det.<br>
&gt; :- implementation.<br>
&gt; :- pragma foreign_type(&quot;Java&quot;, exception, &quot;java.lang.Ex=
ception&quot;).<br>
&gt; :- pragma foreign_proc(&quot;Java&quot;,<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; some_pred(A::in, C::out, _IO0::di, _IO1::uo),<=
br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; [will_not_call_mercury, thread_safe, promise_p=
ure],<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; &quot;try {<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C =3D new Result=
_2.Ok_1&lt;Integer,java.lang.Exception&gt;(A);<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; } catch(java.lang.Exception e) {<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.err.print=
ln(e.getMessage());<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C =3D new Result=
_2.Error_1&lt;Integer,java.lang.Exception&gt;(e);<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; }&quot;).<br>
&gt; main(!IO) :-<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; some_pred(1, C, !IO),<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; (C =3D ok(_) ; C =3D error(_)).<br>
&gt;<br>
&gt; However, when I compile this I get warnings, such as:<br>
&gt;<br>
&gt; mve.m:15: warning: [unchecked] unchecked conversion<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C =3D new Result=
_2.Ok_1&lt;Integer,java.lang.Exception&gt;(A);<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp; ^<br>
&gt;&nbsp;&nbsp; required: Result_2&lt;Integer,Exception&gt;<br>
&gt;&nbsp;&nbsp; found:&nbsp;&nbsp;&nbsp; Ok_1&lt;Integer,Exception&gt;<br>
<br>
Under the current compilation scheme for Java, you are not going to be able=
<br>
to avoid those warnings.&nbsp; Mercury's use of generics in the Java grades=
 is<br>
rather limited.&nbsp; (Much of the generated code effectively works with ra=
w types.)<br>
<br>
&gt; 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 us=
e<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 d=
eal<br>
with constructing result terms in Mercury (as per the attached mve3.m examp=
le).<br>
<br>
(Some time ago I wrote &lt;<a href=3D"https://github.com/juliensf/mercury-j=
ava">https://eur01.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F%2Fgi=
thub.com%2Fjuliensf%2Fmercury-java&amp;data=3D05%7C02%7Cmark.clements%40ki.=
se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C=
0%7C0%7C638938853514514300%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUs=
IlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C6000=
0%7C%7C%7C&amp;sdata=3D67vstV8DKDJ%2BjI3onCJyWrbb3EoVUTvp2XgbhWtOekI%3D&amp=
;reserved=3D0</a>&gt;,
 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>
&gt;<br>
&gt; --- Mark.<br>
&gt;<br>
&gt;<br>
&gt; N=E4r du skickar e-post till Karolinska Institutet (KI) inneb=E4r dett=
a att KI kommer att behandla dina personuppgifter. H=E4r finns information =
om hur KI behandlar personuppgifter&lt;<a href=3D"https://ki.se/om-ki/integ=
ritetsskyddspolicy">https://eur01.safelinks.protection.outlook.com/?url=3Dh=
ttps%3A%2F%2Fki.se%2Fom-ki%2Fintegritetsskyddspolicy&amp;data=3D05%7C02%7Cm=
ark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32b=
e3da1dda043c05d%7C0%7C0%7C638938853514544291%7CUnknown%7CTWFpbGZsb3d8eyJFbX=
B0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUI=
joyfQ%3D%3D%7C60000%7C%7C%7C&amp;sdata=3DlntdNO1jOwSqG1ZPp8lNisKASWQ%2BsNOA=
5UPfHJ9w8oA%3D&amp;reserved=3D0</a>&gt;.<br>
&gt;<br>
&gt;<br>
&gt; Sending email to Karolinska Institutet (KI) will result in KI processi=
ng your personal data. You can read more about KI=92s processing of persona=
l data here&lt;<a href=3D"https://staff.ki.se/data-protection-policy">https=
://staff.ki.se/data-protection-policy</a>&gt;.<br>
&gt; _______________________________________________<br>
&gt; users mailing list<br>
&gt; [email protected]<br>
&gt; <a href=3D"https://lists.mercurylang.org/listinfo/users">https://eur01=
.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F%2Flists.mercurylang.or=
g%2Flistinfo%2Fusers&amp;data=3D05%7C02%7Cmark.clements%40ki.se%7C5ded018e0=
c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938=
853514565787%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMD=
AwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&amp=
;sdata=3DlSTPQxf8lrNqHNC%2B%2BeSgu8o96ZR81THpbOogzCv3BCM%3D&amp;reserved=3D=
0</a><br>
</div>
</span></font></div>
</body>
</html>

--_000_GV3P280MB00011532E99E494471DC785CF012AGV3P280MB0001SWEP_--

--===============0572337526355974738==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KdXNlcnMgbWFp
bGluZyBsaXN0CnVzZXJzQGxpc3RzLm1lcmN1cnlsYW5nLm9yZwpodHRwczovL2xpc3RzLm1lcmN1
cnlsYW5nLm9yZy9saXN0aW5mby91c2Vycwo=

--===============0572337526355974738==--