6 ms·
> external get_internal : map 'a 'b => 'a => Js.undefined 'b = "get" [@@bs.send]; I like languages that are readable. This is starting to look like codegolf. h
by Xeoncross 9y ago
> external get_internal : map 'a 'b => 'a => Js.undefined 'b = "get" [@@bs.send];
I like languages that are readable. This is starting to look like codegolf. https://github.com/reasonml-community/reason-react-hacker-news/blob/master/src/JSMap.re https://github.com/reasonml-community/reason-react-hacker-ne...
- mej10 9y agoreadability is a function of familiarity EDIT: Sorry, I just don't like when people share shallow and negative thoughts about projects that take away from meaningful discussion. Why is the OP comment shallow? Because obviously _some_ people find it readable which would suggest that it is familiarity that is the problem -- not some universal quality of the language.
- cgmg 9y agoReadability is determined by more than familiarity.
- mej10 9y agoThat's true, but since there are many people that find Reason (OCaml) readable I am going to say familiarity is the biggest problem here.
- AlexCoventry 9y agoAgreed, but that is quite readable to someone familiar with ML-based languages: Define a function get_internal which takes a map from objects of type 'a to type 'b, and an object of type 'a, and return a wrapper around objects of type 'b or "undefined"s. Action of get_internal is ""get" [@@bs.send]", which I don't recognize. I would guess it's a javascript interop syntax, which returns a function consistent with get_internal's type signature. You're not constrained to use one-letter names for type variables, or to drop names of argument variables, but it turns out to be very handy, and it only takes reading a couple of hundred lines of code to get comfortable with this notation.
- abiox 9y agodo you have examples in mind that are orthogonal to familiarity?
- cgmg 9y agoLanguage design. Even if you're very familiar with Malbolge, other languages are far more readable.
- piinbinary 9y agoAgreed. I'd argue that the classic `for (int i = 0; i < n; i++)` loop isn't very readable unless you have seen for loops several times, in which case it is pretty easy to read.
- nothrabannosir 9y agoI had almost forgotten about this! But, now that you mention it: yes! I remember being completely mystified by that thing when I first saw it. "Which part does what, when now?" Took a long time to read naturally without conscious deconstruction.
- taeric 9y agoAnd, like it or not, familiarity leverages current experience.
- userbinator 9y agoI encourage everyone who thinks that is unreadable to try APL and read https://news.ycombinator.com/item?id=13565743 https://news.ycombinator.com/item?id=13565743 , or even just try learning a completely different human language --- anything east-Asian is probably going to be initially completely unreadable to those familiar with English and other Latin-family languages.
- chenglou 9y agoThanks for the support. It's hard though, isn't it? We get the same arguments on a weekly basis. Folks categorically refuse to check out OCaml because of its syntax. At one point the Reason creator decided to just go and satisfy the familiarity aspect through syntax change (still ongoing; the above snippet will get better too). It scales easier to settle the debate by just fulfilling the demand rather than encountering each such argument and spend literally years debating about them.
- danenania 9y agoI think fulfilling the demand is the right approach. Elixir seems like a good example to follow in that regard. I mean sure, readability is a function of familiarity, but it's also true that some languages have syntax that is more intuitive than others, which means they are easier to become familiar with. If a language can be human-friendly and approachable like ruby or python without compromising on semantics, then why not try to have it both ways?
- k__ 9y agoI had the impression you are the Reason creator, haha. Are you more like a dev-advocat or something?
- chenglou 9y agoNah I'm the Reason janitor. I declined joining the Reason team because I thought joining messenger was a better way to help Reason move in directions products folk understood; it paid off =) Anyways, since I'm basically doing Reason 24/7 I kinda consider myself to be on the team.
- k__ 9y agoNice move!
- ufo 9y agoMost of that code (the part between the `:` and the `=`) is just the type annotation, which actually acts as documentation :) My guess in this case is that get_internal is a function that receives two parameters (a map with keys of type `a` and values of type `b`, and a key of type `a`) and returns a value of type `b` or `undefined`, if the key is missing. The return is wrapped in an option type so you don't mix the `b`s with the `undefined`s. That said, this is a very weird example to pick because the actual implementation of the function is not in this file. Assuming that in Reason the @@ means the same as in Ocaml, that `@@` is placeholder that tells the code preprocessor where to insert the actual code for the implementation of `get`. The actual logic for the get function would be in the preprocessor.
- yawaramin 9y agoThe `[@@bs.get]` attribute tells the BuckleScript compiler (Reason's JavaScript backend) to emit a JavaScript object 'get' operation wherever its corresponding function (in this case `get_internal`) is used.
- AlexCoventry 9y ago> in Ocaml, that `@@` is placeholder that tells the code preprocessor where to insert the actual code for the implementation of `get`. Where's the documentation for this? I'm only familiar with `@@` as a low-precedence no-op operator which helps to cut down on parentheses.
- ufo 9y agoThe `[@@blah]` syntax refers to the extension-points feature that was introduced in Ocaml 4.02: https://whitequark.org/blog/2014/04/16/a-guide-to-extension-points-in-ocaml/ https://whitequark.org/blog/2014/04/16/a-guide-to-extension-... https://blog.janestreet.com/extension-points-or-how-ocaml-is-becoming-more-like-lisp/ https://blog.janestreet.com/extension-points-or-how-ocaml-is... These can be used to do the sort of metaprogramming that people used to do with camlp4. If you want to search more, the other keyword for googling would be "ppx".
- 9y ago
- jlongster 9y agoThat's not readable. But you're not supposed to write that kind of code normally. You only need that when you need to interop with anything in native JS land, and most of that interop is already done for you so you can just use existing bindings. I think you'll find that any code that deals with foreign function interfaces and interop with the host environment is ugly in any language.
- edanchenkov 9y agoAgree, can someone explain me what does `ignore @@ context##drawImage sprite.img sx sy sw sh dx dy dw dh` mean? I coulnd't find `ignore` in Reason docs from https://github.com/reasonml-community/Mareo/blob/master/src/draw.re#L23 https://github.com/reasonml-community/Mareo/blob/master/src/...
- manu3000 9y agoit calls the method drawImage on the context object. ignore, in OCaml, allows to "cast the expression to void"
- LeonidasXIV 9y agoCasting is a bit of a wrong term here, since that's not at all what happens. `ignore` is just a function that takes any argument and returns `()` (unit). That's useful because sometimes you want to throw away the result of the computation (usually a side effect), so you can just call `ignore` on it and get a `()` value out.
- masklinn 9y ago"ignore" is a standard ocaml function: https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALignore https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasive... val ignore : 'a -> unit it just discards the value it is given. "@@" is a low-binding application operator, similar to Haskell's "$", it's used to avoid parenthesising.
- masklinn 9y agoIt has no readability issue when you know the basic syntax * "external" is an FFI declaration * "get_internal" is the local symbol name * ":" introduces the type signature, so "get_internal" is a function from a "map 'a 'b" (aka Map<A, B> in more C-style generics) and an 'a to a Js.undefined 'b * followed by the external symbol ("get") * and the stuff between brackets is a compiler instruction (an "extension point": https://whitequark.org/blog/2014/04/16/a-guide-to-extension-points-in-ocaml/ https://whitequark.org/blog/2014/04/16/a-guide-to-extension-...)