* [Caml-list] Run time error on exception @ 2018-01-31 21:14 Lorenzo Pomili 2018-01-31 21:35 ` Yawar Amin 2018-02-01 7:10 ` Malcolm Matalka 0 siblings, 2 replies; 6+ messages in thread From: Lorenzo Pomili @ 2018-01-31 21:14 UTC (permalink / raw) To: caml-list [-- Attachment #1: Type: text/plain, Size: 1059 bytes --] Friend of mine, pointed out to me a case in Reason who cause no compile time error but run time error, I had reimplemented similar case in OCaml: exception Generic_exc;; let string_of_string s = string_of_int (int_of_string s) let foo = string_of_string "foo" let fun_exeption x = match x with 0 -> 0 | _ -> raise Generic_exc ;; let bar = string_of_int (fun_exeption 5) both foo and bar don't fail at compile time fail at run time, me and my friend also try to reimplement similar case (the first one with string and int) in Elm, and Elm compiler just say you have to manage exeption, because function can actualy return exeption instead of int. There is a compile params or similar for check this kind of error? is a side effect of have a multi-paradigm? and if is it, how to manage it? I belive I can use OCaml in functional way without just making attention to not use side effect but if use of function like int_of_string can make run time error, I probably approac to OCaml in wrong way, and I wanna understand how to correct it. [-- Attachment #2: Type: text/html, Size: 1186 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Run time error on exception 2018-01-31 21:14 [Caml-list] Run time error on exception Lorenzo Pomili @ 2018-01-31 21:35 ` Yawar Amin 2018-02-01 7:10 ` Malcolm Matalka 1 sibling, 0 replies; 6+ messages in thread From: Yawar Amin @ 2018-01-31 21:35 UTC (permalink / raw) To: Lorenzo Pomili; +Cc: Ocaml Mailing List [-- Attachment #1: Type: text/plain, Size: 2673 bytes --] Hi Lorenzo, On Wed, Jan 31, 2018 at 4:14 PM, Lorenzo Pomili <lorenzo.pomili85@gmail.com> wrote: > Friend of mine, pointed out to me a case in Reason who cause no compile > time error but run time error, I had reimplemented similar case in OCaml: > Runtime errors are very much possible in OCaml/Reason, OCaml does not make any guarantees about runtime errors in general. The only guarantee OCaml makes is that you won't get runtime *type* errors (with a couple of known exceptions). In other words, if your program compiles (passes typechecking) it won't throw a type error when it runs. exception Generic_exc;; > > let string_of_string s = string_of_int (int_of_string s) > > let foo = string_of_string "foo" > > let fun_exeption x = > match x with > 0 -> 0 > | _ -> raise Generic_exc > ;; > > let bar = string_of_int (fun_exeption 5) > > both foo and bar don't fail at compile time fail at run time, > Yeah, it makes sense that they don't fail at compile time, because they pass typechecking according to the OCaml typing rules. > me and my friend also try to reimplement similar case (the first one with > string and int) in Elm, and Elm compiler just say you have to manage > exeption, because function can actualy return exeption instead of int. > Elm tries to disallow *all* runtime errors (not just runtime type errors), so it uses various techniques like using the Maybe and Result types to indicate failed operations. And it makes you handle the possibility of failure up-front. This is really nice, and you can also do this in OCaml, but generally it is opt-in, e.g. you can use List.find (which raises an exception if it doesn't find the item) or List.find_opt (which returns an option containing the result of the list search). There is a compile params or similar for check this kind of error? is a > side effect of have a multi-paradigm? and if is it, how to manage it? > You have to manage it manually, and use various techniques to help. For example, there's a nice pattern-matching technique you can use to return values even after handling exceptions: let bar = string_of_int (match fun_exeption 5 with | int -> int | exception Generic_exc -> 0) > I belive I can use OCaml in functional way without just making attention > to not use side effect but if use of function like int_of_string can make > run time error, I probably approac to OCaml in wrong way, and I wanna > understand how to correct it. > OCaml is a mix between safety and pragmatism, it doesn't take the extreme safety approach that Elm does. There are ups and downs to both approaches, you would have to decide which is best for you. Regards, Yawar [-- Attachment #2: Type: text/html, Size: 3968 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Run time error on exception 2018-01-31 21:14 [Caml-list] Run time error on exception Lorenzo Pomili 2018-01-31 21:35 ` Yawar Amin @ 2018-02-01 7:10 ` Malcolm Matalka 2018-02-04 18:14 ` Lorenzo Pomili 1 sibling, 1 reply; 6+ messages in thread From: Malcolm Matalka @ 2018-02-01 7:10 UTC (permalink / raw) To: Lorenzo Pomili; +Cc: caml-list Exceptions basically mean "runtime error". This is why some APIs use the result type instead of exceptions. So the API could be something like: val int_of_string : string -> (int, err) result where err is some error type. I wrote a blog post about this years ago, the APIs in it might be out dated but the general concept is probably still applies: http://functional-orbitz.blogspot.se/2013/01/experiences-using-resultt-vs-exceptions.html Lorenzo Pomili <lorenzo.pomili85@gmail.com> writes: > Friend of mine, pointed out to me a case in Reason who cause no compile > time error but run time error, I had reimplemented similar case in OCaml: > > > > exception Generic_exc;; > > let string_of_string s = string_of_int (int_of_string s) > > let foo = string_of_string "foo" > > let fun_exeption x = > match x with > 0 -> 0 > | _ -> raise Generic_exc > ;; > > let bar = string_of_int (fun_exeption 5) > > > > both foo and bar don't fail at compile time fail at run time, me and my > friend also try to reimplement similar case (the first one with string and > int) in Elm, and Elm compiler just say you have to manage exeption, because > function can actualy return exeption instead of int. > > There is a compile params or similar for check this kind of error? is a > side effect of have a multi-paradigm? and if is it, how to manage it? > > I belive I can use OCaml in functional way without just making attention to > not use side effect but if use of function like int_of_string can make run > time error, I probably approac to OCaml in wrong way, and I wanna > understand how to correct it. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Run time error on exception 2018-02-01 7:10 ` Malcolm Matalka @ 2018-02-04 18:14 ` Lorenzo Pomili 2018-02-05 10:49 ` Malcolm Matalka 2018-02-05 15:26 ` Yawar Amin 0 siblings, 2 replies; 6+ messages in thread From: Lorenzo Pomili @ 2018-02-04 18:14 UTC (permalink / raw) To: Malcolm Matalka; +Cc: caml-list [-- Attachment #1: Type: text/plain, Size: 1954 bytes --] Thanks for reply, one more question there is a way in OCaml, for know if function can produce side effects? I mean automatic way not read documentation or source code ^^" 2018-02-01 8:10 GMT+01:00 Malcolm Matalka <mmatalka@gmail.com>: > Exceptions basically mean "runtime error". This is why some APIs use > the result type instead of exceptions. So the API could be something > like: > > val int_of_string : string -> (int, err) result > > where err is some error type. > > I wrote a blog post about this years ago, the APIs in it might be out > dated but the general concept is probably still applies: > > http://functional-orbitz.blogspot.se/2013/01/experiences-using-resultt-vs- > exceptions.html > > Lorenzo Pomili <lorenzo.pomili85@gmail.com> writes: > > > Friend of mine, pointed out to me a case in Reason who cause no compile > > time error but run time error, I had reimplemented similar case in OCaml: > > > > > > > > exception Generic_exc;; > > > > let string_of_string s = string_of_int (int_of_string s) > > > > let foo = string_of_string "foo" > > > > let fun_exeption x = > > match x with > > 0 -> 0 > > | _ -> raise Generic_exc > > ;; > > > > let bar = string_of_int (fun_exeption 5) > > > > > > > > both foo and bar don't fail at compile time fail at run time, me and my > > friend also try to reimplement similar case (the first one with string > and > > int) in Elm, and Elm compiler just say you have to manage exeption, > because > > function can actualy return exeption instead of int. > > > > There is a compile params or similar for check this kind of error? is a > > side effect of have a multi-paradigm? and if is it, how to manage it? > > > > I belive I can use OCaml in functional way without just making attention > to > > not use side effect but if use of function like int_of_string can make > run > > time error, I probably approac to OCaml in wrong way, and I wanna > > understand how to correct it. > > [-- Attachment #2: Type: text/html, Size: 2794 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Run time error on exception 2018-02-04 18:14 ` Lorenzo Pomili @ 2018-02-05 10:49 ` Malcolm Matalka 2018-02-05 15:26 ` Yawar Amin 1 sibling, 0 replies; 6+ messages in thread From: Malcolm Matalka @ 2018-02-05 10:49 UTC (permalink / raw) To: Lorenzo Pomili; +Cc: caml-list Lorenzo Pomili <lorenzo.pomili85@gmail.com> writes: > Thanks for reply, one more question there is a way in OCaml, for know if > function can produce side effects? I mean automatic way not read > documentation or source code ^^" Nope. At least not without some third-party tooling, I'm not aware of any. > > 2018-02-01 8:10 GMT+01:00 Malcolm Matalka <mmatalka@gmail.com>: > >> Exceptions basically mean "runtime error". This is why some APIs use >> the result type instead of exceptions. So the API could be something >> like: >> >> val int_of_string : string -> (int, err) result >> >> where err is some error type. >> >> I wrote a blog post about this years ago, the APIs in it might be out >> dated but the general concept is probably still applies: >> >> http://functional-orbitz.blogspot.se/2013/01/experiences-using-resultt-vs- >> exceptions.html >> >> Lorenzo Pomili <lorenzo.pomili85@gmail.com> writes: >> >> > Friend of mine, pointed out to me a case in Reason who cause no compile >> > time error but run time error, I had reimplemented similar case in OCaml: >> > >> > >> > >> > exception Generic_exc;; >> > >> > let string_of_string s = string_of_int (int_of_string s) >> > >> > let foo = string_of_string "foo" >> > >> > let fun_exeption x = >> > match x with >> > 0 -> 0 >> > | _ -> raise Generic_exc >> > ;; >> > >> > let bar = string_of_int (fun_exeption 5) >> > >> > >> > >> > both foo and bar don't fail at compile time fail at run time, me and my >> > friend also try to reimplement similar case (the first one with string >> and >> > int) in Elm, and Elm compiler just say you have to manage exeption, >> because >> > function can actualy return exeption instead of int. >> > >> > There is a compile params or similar for check this kind of error? is a >> > side effect of have a multi-paradigm? and if is it, how to manage it? >> > >> > I belive I can use OCaml in functional way without just making attention >> to >> > not use side effect but if use of function like int_of_string can make >> run >> > time error, I probably approac to OCaml in wrong way, and I wanna >> > understand how to correct it. >> >> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Run time error on exception 2018-02-04 18:14 ` Lorenzo Pomili 2018-02-05 10:49 ` Malcolm Matalka @ 2018-02-05 15:26 ` Yawar Amin 1 sibling, 0 replies; 6+ messages in thread From: Yawar Amin @ 2018-02-05 15:26 UTC (permalink / raw) To: Lorenzo Pomili; +Cc: Malcolm Matalka, Ocaml Mailing List [-- Attachment #1: Type: text/plain, Size: 555 bytes --] Hi Lorenzo, On Sun, Feb 4, 2018 at 1:14 PM, Lorenzo Pomili <lorenzo.pomili85@gmail.com> wrote: > Thanks for reply, one more question there is a way in OCaml, for know if > function can produce side effects? I mean automatic way not read > documentation or source code ^^" > Currently no. But there is ongoing research and development on introducing algebraic effect handlers to OCaml, which will mark functions at the type level (if I understand correctly) as effectful: http://kcsrk.info/ocaml/multicore/2015/05/20/effects-multicore/ Regards, Yawar [-- Attachment #2: Type: text/html, Size: 1043 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2018-02-05 15:27 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-01-31 21:14 [Caml-list] Run time error on exception Lorenzo Pomili 2018-01-31 21:35 ` Yawar Amin 2018-02-01 7:10 ` Malcolm Matalka 2018-02-04 18:14 ` Lorenzo Pomili 2018-02-05 10:49 ` Malcolm Matalka 2018-02-05 15:26 ` Yawar Amin
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox