Live data from Hacker News

Ask HN: Great programming language features, other languages should steal?

news.ycombinator.com

21–30 of 44 posts

Re: Ask HN: Great programming language features, other languages should steal?

#21

This is pretty minor but I like how in Rust you can use constructs like if-statements and match-statements as expressions. For example: let foo = if some_condition { "condition is true" } else { "condition is false" }; let bar = match x { 0 => "string", 1 => "another string", _ => "default" }; It's not super significant but I think it's an elegant syntactic feature.

They got that from lisp. But in lisp it's more than just a syntax trick that only works for a few predefined cases.

And Algol and others. It's a nice feature when available, certainly my preference. I find I can often write things more cleanly given an expression oriented languages than ones dominated by statements.

Re: Ask HN: Great programming language features, other languages should steal?

#23

This is pretty minor but I like how in Rust you can use constructs like if-statements and match-statements as expressions. For example: let foo = if some_condition { "condition is true" } else { "condition is false" }; let bar = match x { 0 => "string", 1 => "another string", _ => "default" }; It's not super significant but I think it's an elegant syntactic feature.

You could do this in GNU C probably at least as far back as 1990. In GNU C, this syntax:

  ({ expr1; expr2; ... ; exprN })
gives you something like a Lisp progn: it's an expression, whose value is exprN.

Complete sample:

  #include 
  
  int main(int argc, char **argv)
  {
    char *foo = (argc > 1) ? ({
                  printf("consequent\n");
                  "condition is true: I have arguments";
                }) : ({
                  printf("alternative\n");
                  "condition is false: I don't have arguments";
                });
    puts(foo);
  }
This is almost certainly because RMS is a Lisp hacker. I.e. why GCC uses Lisp notations internally and functions with "mapcar" in the name and such.

I discovered it by accident sometime in 1993. I wanted the value of the last expression in a braced block. Maybe it can forced into an expression if you just put parentheses on it? By golly, the intuition worked.

Mainly, it is used in writing macros. Speaking of which:

  #include 

  #define ifblock(expr) (expr) ? (
  #define elseblock ) : (
  #define endblock )

  int main(int argc, char **argv)
  {
    char *foo = ifblock (argc > 1) {
                  printf("consequent\n");
                  "condition is true: I have arguments";
                } elseblock {
                  printf("alternative\n");
                  "condition is false: I don't have arguments";
                } endblock;
    puts(foo);
  }

Re: Ask HN: Great programming language features, other languages should steal?

#24

Earlier quoted context omitted.

They got that from lisp. But in lisp it's more than just a syntax trick that only works for a few predefined cases.

And Algol and others. It's a nice feature when available, certainly my preference. I find I can often write things more cleanly given an expression oriented languages than ones dominated by statements.

[deleted]

Re: Ask HN: Great programming language features, other languages should steal?

#25
It's more of a paradigm than a feature but, multiple dispatch à la Julia. One of the creators of Julia makes a strong case that it is the key to libraries that "compose" without being aware of one another:

https://www.youtube.com/watch?v=kc9HwsxE1OY

Re: Ask HN: Great programming language features, other languages should steal?

#27
Nullability built into the type system like Kotlin. one main difference between kotlin optionals and swift options (for example) is that Kotlin nullability is enforced during compile time.

I'm not aware of many languages which do that other than Kotlin.

Re: Ask HN: Great programming language features, other languages should steal?

#28
post #25

It's more of a paradigm than a feature but, multiple dispatch à la Julia. One of the creators of Julia makes a strong case that it is the key to libraries that "compose" without being aware of one another: https://www.youtube.com/watch?v=kc9HwsxE1OY

They got that from lisp.
Post reply on HN