News for hackers. Stuff that matters.
Re:The Go Programming Language, or: Why all C-like languages... by andolanra on Wednesday June 08, 2011 @02:05AM (#2632195)
Considering only pure language design, I have to say that I'd prefer D to Go. A lot of people who talk about Go use some variation on the phrase "small sets of orthogonal features"—a phrase I feel applies to Go only by comparison with, say, C++—and D doesn't succeed in that regard, but I feel like D really fits a lot of the points on the wish-list much more closely (e.g. template metaprogramming, data structures, objects, &c. D's compile-time constructs are incredibly useful without the nastiness of the C preprocessor or C++'s templates.) One thing which draws me to D is the "you can, but you don't have to" attitude it takes towards certain features—for example, there is GC by default, but you can stop using it and do manual memory management if you feel like it's important.

The problem here, and the massive, massive thing keeping me from throwing my full recommendation behind it, is that D fails entirely on #7, because the community is small and so even installing libraries by hand can be tedious. I keep wanting to pull out D for personal projects, but then I come across some obscure, poorly-documented library with few/no alternatives, and after trying to build it for three hours, I give up and switch to something else. Recently, 'something else' has in fact been Go. I still feel like, in an ideal universe, I'd rather program in D than Go, but we do not live in an ideal universe, and of those two, Go is the practical choice. (And, despite my frustrations with Go, it is still better by leaps and bounds than Java and C++.)

Also, quick correction: any dynamic language worth its salt does the same short-circut evaluation with and and or, including Python, Ruby, Scheme, and Common Lisp, so they all have the property ascribed in this writeup to only JS and Perl. In Python, you can change whether instances of a class are 'true' or 'false' values by overloading the __nonzero__ method, which means e.g. empty user-defined data structures could be considered 'false' while non-empty ones could be 'true.' On the other hand, Ruby considers only false and nil to be false values, Scheme considers only #f to be a false value, and Common Lisp considers only nil to be a false value. Aside from individual quibbles about which values are true and false, all of these languages implement an or that returns the first true value it finds, and all of them implement an and that returns the first false value it finds.

EDIT: Lua also allows the short-circuit boolean operators to return values. The only widely-known dynamic language off the top of my head that doesn't do this is Smalltalk. This would be complicated to add to a type system, for relatively little gain, so as far as I know, no typed language allows it.

Re:The Go Programming Language, or: Why all C-like languages... by rsaarelm on Wednesday June 08, 2011 @04:03AM (#2632379)
Especially with Andrei Alexandrescu on board, D is striving for some very interesting stuff with its template metaprogramming system. I think Alexandrescu said in an interview somewhere that the goal is to have a language where you don't ever need to reimplement an algorithm once you've gotten it right once in a library.

This gets particularly interesting with mathematical code. If you have a templatized math function like a linear interpolation function, you can swap in integers, reals or complex numbers without writing new code, and also matrices, vectors or quaternions from another library, provided that they have the algebraic properties the function expects. Go is nowhere near allowing this degree of write-the-algorithm-only-once, as it both lacks generic types and has numeric types as privileged constructs you can't substitute with user-defined ones.

Re:The Go Programming Language, or: Why all C-like languages... by andralex on Wednesday June 08, 2011 @07:27PM (#2635405)
Thanks for your kind words. In D's standard library we consistently attempt to define each and every algorithm in its most general form, with opportunistic specializations wherever available. As a trivial example, startsWith works on average faster on sequences with O(1) length because it can compare the lengths beforehand. D's generic amenities (static if and constrained generics in particular) make it very easy to write code with lots of such micro-specializations effortlessly. You just say "oh, do these guys support the length method? Then if lhs.length < rhs.length return false".

We've managed to reach a very high leverage in std.algorithm (http://d-programming-language.org/phobos/std_algorithm.html) because of that, and there's seldom a need to redo by hand one of its algorithms for efficiency or convenience reasons.

Re:The Go Programming Language, or: Why all C-like languages... by traldan on Wednesday June 08, 2011 @10:55AM (#2633332)
This sounds awesome.
Re:The Go Programming Language, or: Why all C-like languages... by firemanx on Wednesday June 08, 2011 @02:36AM (#2632239)
I was thinking along those lines as well, though the first language that came to my head was actually c# (my second thought was D). It may not be listed due to it's precarious position in the world, but it answers the majority of the points on his list, though still runs in a vm environment. Perhaps that's the distinction.

An aside - using implicit typing in c# you can have an object perform as a boolean in boolean expressions. You can also use implicit typing for more than just booleans as well.

Re:The Go Programming Language, or: Why all C-like languages... by andolanra on Wednesday June 08, 2011 @03:43AM (#2632343)
Typed languages (like C# and Scala) can definitely use non-booleans in boolean expressions; the thing they're lacking here is that you can't return non-booleans from boolean expressions, e.g. you can't have a line like

    config_file = get_config_file() or "~/.my_config"
(which is valid Python; if get_config_file() returns None or False or an empty string, then it sets config_file to a default value instead) because the typing problems get nasty. (You'd have to stipulate that 1. boolean expressions can return non-booleans and that 2. all the arguments to a boolean expression must be of the same type, and, if you want to include a not operator, 3. there are 'canonical' true and false values for every type, so you can evaluate expressions

    String s1 = !"some_string";
    String s2 = !"";
which is why the typing rules would get... uhh, complicated.)
Re:The Go Programming Language, or: Why all C-like languages... by apetrovic on Wednesday June 08, 2011 @04:04AM (#2632382)
Slightly off-topic (you're making another valid point here), you can say this in C#:

    config_file = get_config_file() ?? "~/.my_config";
Re:The Go Programming Language, or: Why all C-like languages... by randallsquared on Wednesday June 08, 2011 @08:30AM (#2632836)
Perhaps because I don't understand type theory, this doesn't seem especially hard to me. Every type should be a subtype of boolean, right? Otherwise you couldn't evaluate truth or falsity on something like a string in the first place.

Wouldn't the 'not' function signified by your operator just return a basic boolean (for any argument which was a subtype of boolean, which would be any argument...)?

[To be clear, "go read 'Book That Will School You' " is an acceptable answer to me, if it really will]

Re:The Go Programming Language, or: Why all C-like languages... by timtadh on Wednesday June 08, 2011 @08:59AM (#2632900)
here is a book that will school you: http://www.amazon.com/Types-Programming-Languages-Benjamin-P...

It is a great book, but a little dense. Its example language is a typed lambda calculus. This is both good and bad. The good: you can evaluate most expressions in your head or with a pencil. The bad: sometimes it is difficult to read without evaluating the expressions.

It is heavy on proofs and mathematics, but it does what it says: it introduces you to the basics of type theory as applied to programming languages.

Edit: an amazon reviewer pointed at http://www.amazon.com/Foundations-Object-Oriented-Languages-... as an alternative book. I haven't read it but it does look useful.

Re:The Go Programming Language, or: Why all C-like languages... by andolanra on Wednesday June 08, 2011 @03:05PM (#2634556)
I second the commenter who recommended Types and Programming Languages, but the short answer is, "Sure, if your language has subtyping, and you want to arrange the inheritance hierarchy in that way." It would solve it a little bit better because the type of

    x || (y && z)
would default to being the 'most general' type of all its arguments, which could be Bool but might be more specific in certain circumstances. But this of course only works if your language has some kind of subtype relation, which is not necessarily true of every language—it is not, for example, true of Go, or most typed functional languages that I know of—and it still ends up having typing rules like

    t1 : A    t2 : B    A <: C    B <: C
    ------------------------------------
                t1 || t2 : C
It also assumes that the Boolean class is implemented as a single class with no subtypes for True and False—i.e. not like Ruby's TrueClass and FalseClass; you'd have an instance variable or something which tells you whether an instance is True or False—because if you implemented it with singleton instances of a True class and a False class, then you'd bifurcate your whole object hierarchy, and it also assumes that there can be no type 'more general' than Booleans, because if there is something 'above' Boolean in the hierarchy, then you'd have to rewrite the rule as

    t1 : A    t2 : B    A <: C    B <: C   C <: Bool
    ------------------------------------------------
                     t1 || t2 : C
and... well, it does get a little complicated.
Re:The Go Programming Language, or: Why all C-like languages... by lurker14 on Wednesday June 08, 2011 @01:53PM (#2634201)
Here is how it can be done in Haskell.

Instead of using a Boolean, use a type that means what you actually want: You want to short-circuit combine two (or more) values, returning the first one that is valid, without evaluating the later ones.

You need a type that represents a possibly-unavailable value. "Boolean" is not that type. "Maybe" is Haskell's type-safe "Nullable" type. It has two kinds of values: "Nothing" or "Just a", where "a" is a value of any type.

Some quick simplified definitions of Haskell terms: "Control.Monad" is Haskell's generalization of "flow control" "mplus" is Haskell's generalization of "or" (as it means in Perl/Python).

backticks are used to make a regular prefix function into an infix operator.

"undefined" is like Perl's "die" or Java's uncatchable Exception, used here to show where short-circuit happens.

ghci is a Haskell interpreter.

  % ghci

  > import Maybe

  > import Control.Monad

  > undefined `mplus` Just 1

  *** Exception: Prelude.undefined

  > Just 1 `mplus` undefined
  
  Just 1

  > Nothing `mplus` (Just 1) `mplus` undefined

  Just 1 

  > Nothing `mplus` (Just 1) `mplus` (Just 2) 

  Just 2
This web page goes into a bit more detail on this technique: http://www.engr.mun.ca/~theo/Misc/haskell_and_monads.htm

It's slightly complex to understand, since it so generalized, but in practice it makes for simple, safe code.

Re:The Go Programming Language, or: Why all C-like languages... by nandemo on Wednesday June 08, 2011 @02:25AM (#2632218)
I find it funny that in a "best C-like language" post he doesn't even mention D.
Re:The Go Programming Language, or: Why all C-like languages... by billforsternz on Wednesday June 08, 2011 @02:42AM (#2632247)
He makes an indirect sarcastic reference to it via a link; look for the text "stay in the spheres of irrelevancy".
Re:The Go Programming Language, or: Why all C-like languages... by uriel on Wednesday June 08, 2011 @03:47AM (#2632352)
Go is C-like, D is C++-like.
Re:The Go Programming Language, or: Why all C-like languages... by andralex on Wednesday June 08, 2011 @07:08PM (#2635372)
I disagree with such umbrella assessments. Besides, forcing people to use garbage collection is very unlike C.
Re:The Go Programming Language, or: Why all C-like languages... by Klinky on Wednesday June 08, 2011 @06:33AM (#2632626)
Oh no, there's a C++- now :p?
Re:The Go Programming Language, or: Why all C-like languages... by uriel on Wednesday June 08, 2011 @12:02PM (#2633671)
Many people consider Java to be C++--
Re:The Go Programming Language, or: Why all C-like languages... by scythe on Wednesday June 08, 2011 @07:59AM (#2632760)
What about Vala? It deserves more attention, if you ask me.
Re:The Go Programming Language, or: Why all C-like languages... by bartonfink on Wednesday June 08, 2011 @09:09AM (#2632928)
Can I ask what you like about Vala? Its wikipedia page is pretty sparse, and I'd never heard of it until this post.
Re:The Go Programming Language, or: Why all C-like languages... by scythe on Wednesday June 08, 2011 @10:14AM (#2633144)
It's practical. It binds very well to old C code; consequently, it avoids all the library-binding problems that plague D and (iirc) Go. It does well enough at backwards-compatibility that it even comes with a nice Lua binding:

http://live.gnome.org/Vala/LuaSample

Granted, Vala is what it is -- a language built for GLib -- but I think we've had enough languages try to take over the world. It's not unimpressive for a language just five years old and almost entirely community-developed to have a lot already being writen in it:

http://live.gnome.org/Vala/Documentation#Projects_Developed_...

including Ubuntu's new user interface. Since it only really depends on GLib and gtk, it's cross-platform enough for most purposes, though the majority of projects written thusfar in Vala have only targeted X-based desktops.

Re:The Go Programming Language, or: Why all C-like languages... by adamdr on Wednesday June 08, 2011 @07:40PM (#2635436)
Note that D can call C directly too; all you have to do is add the function's prototype to an extern(C) block in your D code. You can cheat on the prototype too, for example, using void* rather than spell out a struct pointer type, if you're in a rush.

There's also pragmas for adding the needed library to the compile command line.

It's actually pretty easy to use.

D's C++ binding support, on the other hand, leaves quite a bit to be desired... I always do extern C functions to bind them together.

Re:The Go Programming Language, or: Why all C-like languages... by notJim on Wednesday June 08, 2011 @12:03PM (#2633674)
PHP's boolean operators return booleans instead of values :(.