Too much code when using Map

Hey, I am using maps and it turns out that it takes a lot of code to write something simple. For example, even though I want to use find function, I need to specify exact module. For example, if I have a

module StringMap = Map.Make(String)
module IntMap = Map.Make(Int)

then, in order to find something:

let x = IntMap.find k string_map 
let y = StringMap.find k int_map

Is there a way to avoid using different modules for find, and maybe use something general like Map.find ? Shall I switch to Base: I believe in Base one can use Map.find everywhere.

Replacing IntMap.find or StringMap.find by Map.find represents a difference of 3 or 6 characters. Not sure I follow how that is “a lot of code”… :slight_smile:



You can always use module alias:

module Sm = StringMap

Sm.find …

Also, scoped open is a treasure:

let x = StringMap.( find … )

1 Like

There’s this saying that most importantly your code should be easy to read. Code readability is a tough question and answers vary from one person to another, but I think it doesn’t reduce to writing concise code. Having the module names hinting at the types that are used in some code is in my experience very helpful when you try to understand it. IMO the few extra-characters you type are absolutely worth the trouble in the long run, and personally I’d prefer to read a version with the explicit StringMap and IntMap names.