Created
February 12, 2014 23:24
-
-
Save hkoba/8966667 to your computer and use it in GitHub Desktop.
Why this ocaml code emits warning 6 for String.sub? (labels were omitted in the application of this function.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* -*- coding: utf-8 -*- *) | |
open Core.Std | |
module Strrange = struct | |
let (//) opt def = match opt with Some x -> x | None -> def | |
let min x y = if x < y then x else y | |
type t = {base: string; mutable start: int; mutable finish: int} | |
let create ?(start=0) ?finish ?(empty=false) str = | |
{base = str; start = start; | |
finish = | |
if empty then start | |
else min (finish // String.length str) (String.length str)} | |
let of_string str = create str | |
let to_string ?(offset=0) {base; start; finish} = | |
let start = start + offset in | |
String.sub base start (finish - start) | |
end |
Thank you so much, so the problem is about the types not being identical ?
Yes and No. The problem of my code is type mismatching as you said. The problem I (indirectly) criticized here was that how hard it was to understand one warning message, which was caused by the split situation of the learning environment of OCaml between its standard and Core (which Real World OCaml is based on) at that time.
Oh, okay
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the warning from ocaml:
Finally I found
Core.Std.String.sub "foobar"
has following type:So, changing line 20 as following cleared this warning:
Umm... bit frustrated.