FWIW, I personally would use FILEptr_is_null to check if fptr is null, rather than ptrcast and then a comparison to 0
On 3/12/19 2:22 PM, Hongwei Xi wrote:
> Here is a quick fix:
> extern
> praxi
> clear{a:t0ype}(x: INV(a)): void
>
> fun open_close(): void =
> let
> val fptr = fopen("hello.dats", file_mode_r)
> val p = ptrcast(fptr)
> in
> if p = 0
> then
> let
> prval () = FILEptr_free_null(fptr)
> in
> end
> else
> let
> val (pf|rc) = fclose(fptr)
> val () = assertloc(rc = 0) // fclose is expected to succeed
> prval None_v() = pf
> prval () = clear(fptr)
> in
> end
> end
>
> ######
>
> The above shows a C-style of dealing with IO in ATS. What I like to
> recommend
> is to deal with IO via the use of linear streams:
>
> https://github.com/ats-lang/ATS-CodeBook/tree/master/RECIPE/ReadFromSTDIN
>
> On Tue, Mar 12, 2019 at 2:17 PM Shimin Guo <[email protected]
> <mailto:[email protected]>> wrote:
>
> I wrote the following code
>
> |
> #include "share/atspre_define.hats"
> #include "share/atspre_staload.hats"
>
> staload "libats/libc/SATS/stdio.sats"
>
> fun open_close(): void =
> let
> val fptr = fopen("hello.dats", file_mode_r)
> val p = ptrcast(fptr)
> in
> if p = 0
> then
> let
> prval () = FILEptr_free_null(fptr)
> in
> end
> else
> let
> val (pf|rc) = fclose(fptr)
> val () = assertloc(rc = 0) // fclose is expected to succeed
> prval None_v() = pf
> in
> end
> end
>
> |
>
>
> and got the following compiler error:
>
>
> /home/sguo/ats/file.dats: 242(line=13, offs=7) -- 307(line=16,
> offs=10): error(3): the dynamic variable [fptr$4718(-1)] is
> consumed but it should be retained with the type
> [S2Eapp(S2Ecst(ptr_addr_type); S2Evar(l$8617$8618(14269)))] instead.
> patsopt(TRANS3): there are [1] errors in total.
> exit(ATS): uncaught exception:
>
> _2home_2hwxi_2Research_2ATS_2dPostiats_2src_2pats_error_2esats__FatalErrorExn(1025)
>
>
>
> Why does it say fptr should be retained?
> --
> You received this message because you are subscribed to the Google
> Groups "ats-lang-users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to
> [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/ats-lang-users.
> To view this discussion on the web visit
>
> https://groups.google.com/d/msgid/ats-lang-users/8246dd5f-fb2f-497e-add4-5c808768ed50%40googlegroups.com
>
> <https://groups.google.com/d/msgid/ats-lang-users/8246dd5f-fb2f-497e-add4-5c808768ed50%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "ats-lang-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/ats-lang-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLrozrnEK_FiFB5ZyFU%2BEYU%3DZ0nYjZOvbcB5%2BsKcseeSsg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLrozrnEK_FiFB5ZyFU%2BEYU%3DZ0nYjZOvbcB5%2BsKcseeSsg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
--
You received this message because you are subscribed to the Google Groups
"ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ats-lang-users/9a5a4f56-4fdb-ffc1-ff43-10f1d87fa3e6%40iohk.io.
signature.asc
Description: OpenPGP digital signature
