On Tue, Mar 12, 2019 at 12:23 PM Hongwei Xi <[email protected]> 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
>
I'm trying to implement tail in ATS as a practice. It seems the streams
interface may not be a good fit in this case. Is there a better way to do
it than the C-style way? (read the last N bytes into a buffer, count the
number of new lines from the end)

>
>
> On Tue, Mar 12, 2019 at 2:17 PM Shimin Guo <[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].
>> 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/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].
> 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/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/CAA6Z%2B7BbLK9Fvg0gNN%2B6oCEJMY_E6D610AG8Pf8vG5QZSaekwA%40mail.gmail.com.

Reply via email to