4 ms·
Sometimes I wonder why do all the programming language designers prefer to invent several different syntaxes for one concept... From the third slide: we can se
by tomp 15y ago
Sometimes I wonder why do all the programming language designers prefer to invent several different syntaxes for one concept...
From the third slide: we can see two different function definition syntaxes, a "normal" style and a "ruby" style. I really like the way of JavaScript, CoffeeScript and OCaml that have a single unified function syntax.
Also on this slide: why have a special syntax for importing submodules from modules/crates? Again, CoffeeScript excels here, an import is simply a variable declaration:
{int, vec} = require('std')
Furthermore, it has always bothered me why generic types use <> brackets. I can understand it in Java and C# (where types are declared before values/functions), but in Rust, it's really unnecessary to introduce the 4th type of brackets into the language (apparently, they used [], but latter changed it to <>... I would really like to know why.)
- Dn_Ab 15y agoI am not trying to correct but clarify what you mean by unified syntax. In OCaml you can say let rec func = ... , let func = ..., let func = function |... | _->... and let func = fun ... -> ... Now I find that each of these variations make good sense and would call them unified facets but they tripped me up many years ago when I was first learning the language.
- tomp 15y agoThat's true, and there is also let func a b c = ..., and most of these are just syntax sugar, mainly useful at the module level. The important thing is, though, that the function declaration syntax is the same as the "inline" function syntax, I can easily say: let f = function 0 -> false | 1 -> true use (f) and then refactor the code into use (function 0 -> false | 1 -> true) That's the consistency I'm talking about...
- philh 15y ago> {int, vec} = require('std') I don't know coffeescript, but this looks to me like "std exposes two things, and I am bringing them into this namespace as int and vec". Whereas the rust code looks like "std exposes int and vec, which I am bringing into this namespace, and possibly other things which I am not".
- yesbabyyes 15y agoWhat this means, in CoffeeScript, is that std exposes int and vec, which I am bringing into this namespace, and possibly other things which I am not. If I want to use all of std, I would have to write std = require('std') and access std.int, std.vec and so on.
- philh 15y agoThanks for clarifying. I think I like rust version better: when I see an '=' sign, I assume that the RHS returns something which is assigned to the LHS, and something doesn't depend on the LHS. Coffeescript violates that, and it feels much more like 'special syntax'. But without knowing either language, I won't go further than "this seems weird".
- moomin 15y agoActually, the genius of the coffeescript version is that it's just a use case of the general destructuring syntax. So { x, y } = { x : 3, y : 5, z : 3 } is perfectly valid as well. Once you get the hang of coffeescript's destructuring, it's insanely powerful.
- yesbabyyes 15y agoI don't view it as that the something returned depends on the left hand side, rather that the left hand side picks what it wants out of that. It's also useful in arrays: [x, y] = [1, 2] With splats: [first, second, rest...] = contenders
- beagle3 15y agoEven in C++, C# and Java this choice makes no sense. In both C++ and C# it leads to ambiguous constructs and parsers beyond the realm of yacc & friends. The Java syntax was carefully designed to avoid those ambiguities, but still... Simply using [] or {} for template arguments would have been way, way better for everyone.
- pcwalton 15y ago"From the third slide: we can see two different function definition syntaxes, a 'normal' style and a 'ruby' style. I really like the way of JavaScript, CoffeeScript and OCaml that have a single unified function syntax." Functions declared at top-level with the |fn| keyword can be mutually recursive and don't close over anything by default. Blocks, however, are unique in that they can close over stack variables (we statically ensure that they can't escape) and can't be mutually recursive. At the moment, blocks can only appear in function argument position. So, although both |fn| functions and blocks are functions in some sense, they have different enough semantics that we felt that different syntax helped to convey the distinction. "Also on this slide: why have a special syntax for importing submodules from modules/crates? Again, CoffeeScript excels here, an import is simply a variable declaration:" Well, in CoffeeScript, imports are actually statements that are evaluated at runtime. In Rust, they're compile-time constructs. Furthermore, using a separate syntax gives us more power — you can say |import *|, you can say things like |import std::{hashmap::map, vec}|, etc. "Furthermore, it has always bothered me why generic types use <> brackets. I can understand it in Java and C# (where types are declared before values/functions), but in Rust, it's really unnecessary to introduce the 4th type of brackets into the language (apparently, they used [], but latter changed it to <>... I would really like to know why.)" Because you can use generic type instantiation in value position as well (e.g. none::<int>), and [] was preventing us from using [] for array indexing (previously, you wrote v.(0) for v[0]). I also thought it'd be more familiar for C++, C#, and Java programmers.