News for hackers. Stuff that matters.
Re:Language Design: Use 'ident: Type' not 'Type ident' by mariodiana on Friday May 01, 2020 @06:02PM (#23046996)
I was just thinking about pointer syntax today, and if you ask me, there are a lot of problems that could be avoided if language designers took a page out of Guido van Rossum's book and extend his idea of forced spacing.

Take the first example you've given, and let's just talk about variable declaration:

    int* a;
    int * a;
    int *a;
That's all the same. That's wrong. Obviously, you can write a lexer and parser that doesn't give a crap, but the human mind does. If you think it doesn't, that's only because you've internalized the various cases.

It should be this:

    int* a;
The type we're talking about is a pointer to a variable of type int: in other words, "a" is an int-pointer. If you were to create a macro, you'd do something like this:

    #define int_ptr int*
Do you see what I'm getting at? The star in this case is a suffix, equivalent (in our minds) to "_ptr". Conceptually, it doesn't belong anywhere else than attached to the type. It's a compound type, conceptually.

Now, take the star being used in a different context:

    int b = 10;
    int* a = &b;
    printf("%d\n", *a);
There, though we see the same character, it's a completely different thing. It's a dereference operator. Conceptually, it belongs attached to the pointer variable it is dereferencing; and since the star is already used in one context as a suffix, here it should be used as a prefix.

This is no good:

    printf("%d\n", * a);
It doesn't matter that "this compiles." That's not what this is about.

Many C programmers (and programmers in other languages, even Python) are used to writing things like this:

    int c = x*y;
That's wrong. Sure, the lexer and parser don't care. But that makes the language worse, for the human operator. "But it saves space!" Spare me.

The thing with this one example, using the star, is that what we have is the equivalent of a homonym. We have one sign that is actually three different words. Mandating spacing removes the ambiguity you're complaining about.

C is what it is, but if we imagine someone were going to write it today, they should incorporate the above and mandate spacing. For the sake of the humans. "ident type" is not the only solution.

Re:Language Design: Use 'ident: Type' not 'Type ident' by dllthomas on Friday May 01, 2020 @10:39PM (#23048661)
There's a perspective that doesn't seem to be noted yet, which is that

   int *f
is declaring that

   *f
will be an int.

This perspective addresses why the star belongs with the name, why it's star instead of ampersand, why you need to repeat the star for multiple variables, why the brackets goes after the name for arrays, and it will sort of get you where you need to go with function pointers (although there's an automatic promotion which means things will work at the call site that won't work for the type).

This isn't to say alternative constructions mightn't be a better choice in a new language, but it's much more parsimonious when considering C than memorizing a bunch of special cases.

Re:Language Design: Use 'ident: Type' not 'Type ident' by kevin_thibedeau on Saturday May 02, 2020 @01:54AM (#23049636)
Then someone comes along and typedefs a pointer to hide the "scary" double pointer syntax.
Re:Language Design: Use 'ident: Type' not 'Type ident' by dllthomas on Saturday May 02, 2020 @04:24PM (#23054634)
I don't think that actually breaks things. A typedef isn't purely syntactic substitution like a macro.

If we have

    typedef float *floatp;
then the following compiles:

    float x, y;
    floatp xp = &x, yp = &y;
while the following does not:

    float x, y;
    float* xp = &x, yp = &y;

You have defined a new type (which happens to be equivalent to an old type - note that we don't get nominal type-checking from typedefs); the above logic still holds.
Re:Language Design: Use 'ident: Type' not 'Type ident' by cyphar on Friday May 01, 2020 @10:27PM (#23048591)
People have been arguing about where the asterisk should be for a very long time. The main counter-argument to the construction you used is that multiple variable declaration immediately looks ugly given the current rules of C variable declaration:

    int* a,* b; // ??
And that's usually why most C style guidelines use

    int *a, *b;
Now, if we treated "int<asterisk>" as the full type name in the syntax then you get a much nicer result:

    int* a, b; // much nicer!
Though that does have its own tradeoffs. But that would require actually changing the C syntax, at which point you might as well make it the postfix syntax:

    var a, b int*;
Re:Language Design: Use 'ident: Type' not 'Type ident' by klyrs on Friday May 01, 2020 @07:14PM (#23047471)
> It should be this:

> int* a;

I must disagree... the following sends the wrong message the reader:

  int* a, b;
Though I also agree that dereferencing is best without a space. Perhaps the correct suggestion is to not declare pointers and instances on the same line, but that's a convenience that many seem to enjoy
Re:Language Design: Use 'ident: Type' not 'Type ident' by tomjupiter on Friday May 01, 2020 @07:49PM (#23047690)
I think mariodiana is describing a hypothetical language where a and b would both be int-pointers in this case.
Re:Language Design: Use 'ident: Type' not 'Type ident' by WalterBright on Friday May 01, 2020 @08:22PM (#23047871)
To declare two pointers to int in C:

    int *a, *b;
In D:

    int* a, b;
The use of whitespace makes the distinction clear, although the parser doesn't care.
Re:Language Design: Use 'ident: Type' not 'Type ident' by klyrs on Friday May 01, 2020 @08:40PM (#23048007)
C is just riddled with mistakes and this one finally gelled with me. Python taught me that whitespace is great for syntax and, in this case, I'm quite convinced that it should be used more firmly in most languages.
Re:Language Design: Use 'ident: Type' not 'Type ident' by andreareina on Saturday May 02, 2020 @01:10AM (#23049417)
Python's syntax precludes works against a lot of things, like first-class lambdas, case (yes it can be emulated with if/elif, but it's not the same, even without fallthrough), pattern-matching in general, or assigning the result of a method-chaining pipeline to a variable.

I use python at $dayjob, and I run into the limitations of whitespace as syntax all the time.

Re:Language Design: Use 'ident: Type' not 'Type ident' by klyrs on Saturday May 02, 2020 @12:39PM (#23052899)
Yeah, the switch thing is a mindboggle, but the associated pep makes it very clear that/why the problem is quite unsolvable.

https://www.python.org/dev/peps/pep-3103/

It's quite frustrating as there are a lot of situations where a simple switch would be so nice...

Re:Language Design: Use 'ident: Type' not 'Type ident' by dirtydroog on Friday May 01, 2020 @09:53PM (#23048396)
I find the opposite, I can't stand it. Give me curly braces over arbitrary indentation any day.
Re:Language Design: Use 'ident: Type' not 'Type ident' by mhh__ on Friday May 01, 2020 @10:33PM (#23048622)
I think a better (subjective of course) lesson might be to enforce a style.

I have been thinking about making a toy compiler (I wanted to write a borrow checker) that treats bad code as an error, solely aimed at numerical code - I have recently had "Scientific Programming in Python for physics etc." inflicted on me.

Slight tangent, but I think if Haskell enforced some kind of whitespace a la Python it would be much more approachable in real codebases (Haskell is usually quite readable if you are just translating mathematics into code but it - to me at least - feels dreadful as a productive language because of the way a lot of functions seem to be dumped onto into the text editor in a lot of the code I have read)

Re:Language Design: Use 'ident: Type' not 'Type ident' by iso-8859-1 on Saturday May 02, 2020 @12:24AM (#23049181)
What do you mean? Whitespace does mean something in Haskell, see http://echo.rsmw.net/n00bfaq.html .
Re:Language Design: Use 'ident: Type' not 'Type ident' by dllthomas on Saturday May 02, 2020 @04:33PM (#23054690)
Not really relevant to the broader discussion, but Haskell's whitespace sensitivity is defined in terms of automatic insertion of braces and semicolons, and you can write it that way instead if you want. A few people do write Haskell that way (SPJ maybe?) but the community is mostly united around using whitespace. That said, it's useful to know about the braces and semicolons if you're generating Haskell code, because that's often easier. Also occasionally at the GHCI prompt, where it will let you keep things on one line.
Re:Language Design: Use 'ident: Type' not 'Type ident' by todd8 on Saturday May 02, 2020 @06:27AM (#23050829)
As in Python, whitespace in the form of indentation matters in Haskell.
Re:Language Design: Use 'ident: Type' not 'Type ident' by mariodiana on Saturday May 02, 2020 @05:18PM (#23055002)
If I'm describing a hypothetical language, then I'm actually describing a hypothetical language where you wouldn't declare more than one variable per line.

It's been a long, long time since C required a programmer to declare his variables at the top, and best practice argues that you declare a variable as close to its use as possible. So, there really isn't the same case for multiple variables on one line, whereas it may have been a little more forgivable, once upon a time.

Moreover, when I was first learning C, I learned from an O'Reilly book by Steve Oualline. I remember him saying, when it came to operator precedence, that coding style that relied on the rules was a really bad idea. I think he said something like, "Multiplication and division come before addition and subtraction, and use parentheses for everything else."

My bottom line is C has a little too much "convenience" to it. (Granted, to my taste.) It goes back to my van Rossum comment. I'm against special cases and loosey-goosey stuff.

Re:Language Design: Use 'ident: Type' not 'Type ident' by ufo on Friday May 01, 2020 @08:54PM (#23048087)
The problem in C is more than just the `*`. Consider arrays, where C uses `int xs[];` instead of `int[] xs;`. There is no way to use define to declare an int_array type like you did with int_ptr. (Function types also exibit a similar problem)
Re:Language Design: Use 'ident: Type' not 'Type ident' by a1369209993 on Saturday May 02, 2020 @01:45AM (#23049590)
There are four possible spacings to consider:

  foo*bar; // multiplies foo and bar
  foo* bar; // declares bar as pointer-to-foo
  foo *bar; // declares *bar as foo (so bar is still a foo*)
  foo * bar; // multiplies foo and bar
These are all unambigous and there are only two semantics between them. You also have:

  foo* bar,baz; // baz is a pointer
  foo *bar,baz; // baz is a foo
  foo *bar,*baz; // baz is pointer again
  foo* bar,*baz; // baz is now a pointer *to* a pointer
This is all obvious - or at worst unambigous - to a person reading the actual code (without trying to correct for the idiosyncracies of a parser), so the language should either match that or spit out a warning about unsupported spacing.

> [`int c = x*y;` is] wrong.

No, that's unambigously multiplication.

Re:Language Design: Use 'ident: Type' not 'Type ident' by earthboundkid on Friday May 01, 2020 @09:58PM (#23048422)

    A pointer to an int "should" be &int, not int*. That we use *, the 
    dereference operator, to indicate pointers is wrong. * in a type means it's an 
    address, but * in a value means it's not an address. That's nuts! Make it 
    consistent and use & both places. If you need to have a distinction between 
    refs and pointers, it should be that pointers are nullable refs: &int? or some 
    such.

    Edit: How to escape asterisks in HN?
Re:Language Design: Use 'ident: Type' not 'Type ident' by mariodiana on Friday May 01, 2020 @10:18PM (#23048530)
I think you may be right, &int would be better than int*.

I did not want to stray too far from what C does now, for the sake of argument. I just wanted to say that the ambiguity in C is often because it's so loosey-goosey with whitespace.

Re:Language Design: Use 'ident: Type' not 'Type ident' by saagarjha on Friday May 01, 2020 @10:06PM (#23048473)
I’ve had luck with not putting something directly after the asterisk.
Re:Language Design: Use 'ident: Type' not 'Type ident' by earthboundkid on Saturday May 02, 2020 @12:58PM (#23053079)
Apparently HN does not have an escape character which seems like a real oversight. :-/