Skip to content

Instantly share code, notes, and snippets.

@hkoba
Created February 12, 2014 23:24
Show Gist options
  • Save hkoba/8966667 to your computer and use it in GitHub Desktop.
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.)
(* -*- 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
@hkoba
Copy link
Author

hkoba commented Feb 12, 2014

Here is the warning from ocaml:

File "why_labels_omitted.ml", line 20, characters 4-14:
Warning 6: labels were omitted in the application of this function.

Finally I found Core.Std.String.sub "foobar" has following type:

# Core.Std.String.sub "foobar";;
- : pos:int -> len:int -> string = <fun>

So, changing line 20 as following cleared this warning:

    String.sub base ~pos:start ~len:(finish - start)

Umm... bit frustrated.

@Fb921
Copy link

Fb921 commented May 15, 2020

Thank you so much, so the problem is about the types not being identical ?

@hkoba
Copy link
Author

hkoba commented May 16, 2020

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.

@Fb921
Copy link

Fb921 commented Jun 1, 2020

Oh, okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment