{"action":"closed","issue":{"url":"https://api.github.com/repos/elm-lang/elm-plans/issues/5","labels_url":"https://api.github.com/repos/elm-lang/elm-plans/issues/5/labels{/name}","comments_url":"https://api.github.com/repos/elm-lang/elm-plans/issues/5/comments","events_url":"https://api.github.com/repos/elm-lang/elm-plans/issues/5/events","html_url":"https://github.com/elm-lang/elm-plans/issues/5","id":94789030,"number":5,"title":"Proposal: Local imports","user":{"login":"keleshev","id":619158,"avatar_url":"https://avatars.githubusercontent.com/u/619158?v=3","gravatar_id":"","url":"https://api.github.com/users/keleshev","html_url":"https://github.com/keleshev","followers_url":"https://api.github.com/users/keleshev/followers","following_url":"https://api.github.com/users/keleshev/following{/other_user}","gists_url":"https://api.github.com/users/keleshev/gists{/gist_id}","starred_url":"https://api.github.com/users/keleshev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keleshev/subscriptions","organizations_url":"https://api.github.com/users/keleshev/orgs","repos_url":"https://api.github.com/users/keleshev/repos","events_url":"https://api.github.com/users/keleshev/events{/privacy}","received_events_url":"https://api.github.com/users/keleshev/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/elm-lang/elm-plans/labels/proposal","name":"proposal","color":"5319e7"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-07-13T19:08:11Z","updated_at":"2015-08-25T01:25:48Z","closed_at":"2015-08-25T01:25:48Z","body":"Like Elm, OCaml does not allow overloading functions and operators. However, it is desirable to use the same function names and operators in different context. For example, you might want to use operators `+`, `*`, `^` and so on for vector math. \r\n\r\nRight now you are forced to define more obscure operators such as for example `+.`, `-.`, `^.` for vector math.\r\n\r\nOCaml solves this with \"local import\", which they call [\"local open\"](https://realworldocaml.org/v1/en/html/files-modules-and-programs.html#opening-modules). It allows to import contents of a module in a limited scope. The syntax for it is `<module name>.(<expression)`, for example:\r\n\r\n```ocaml\r\nVector.(v + 2.0 * w)\r\n```\r\n\r\nHere, inside the parenthesis operators `+` and `*` operate on vectors (`*` multiplies a vector by a factor). Outside the parenthesis these operators stand for regular math.\r\n\r\nHere's an example on how this could look in Elm:\r\n\r\n```elm\r\n-- vector.elm\r\nmodule Vector where\r\n\r\ntype alias Vector = { x: Float, y: Float }\r\n\r\n(*) : Float -> Vector -> Vector\r\n(*) factor vector =\r\n  { vector | x <- Float.(vector.x * factor)\r\n           , y <- Float.(vector.y * factor) }\r\n\r\n(+) : Vector -> Vector -> Vector\r\n(+) v w = \r\n  { v | x <- Float.(v.x + w.x)\r\n      , y <- Float.(v.y + w.y) }\r\n\r\n-- test.elm\r\nv = { x=5, y=5 }\r\nw = { x=0, y=2 }\r\n\r\nVector.(v + 2 * w) == { x=0, y=20 }\r\n```\r\n\r\nIn this example we also use local import to access `Float` operators in order to distinguish them from `Vector` operators inside `vector.elm`."}}