Announcement

Collapse
No announcement yet.

John Carmack's Comments On C/C++

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #31
    Originally posted by mark45 View Post
    What exactly got you disappointed in D? Just curious.
    Very high number of keywords for a starting language. No way to avoid garbage collection. Seemed too much like "design by fad".

    Comment


    • #32
      Originally posted by h**2 View Post
      C++(11) does *exactly* this:
      Code:
      template<typename T>
      T foo(const T x &, const int y) 
      {
          return x * y;
      }
      
      int main()
      {
          char  s[] = "text"; # 's' is text
          auto i = 0;      # 'i' is an int
          auto f = 0.0 ;   # 'f' is a float
      
          auto r = foo(s, i) ;# error: can't pass text as a first parameter
          auto r2 = foo(f, i) ;# works: because compiler can multiply a 'int' and a 'float'
          auto r3 = foo(i, i) ;# works: compiler can compile 'int' and 'int'
          return 0;
      }
      That auto stuff is neat. I can't understand why it took them so long, should have been added to the spec together with the templates.

      Comment


      • #33
        Originally posted by h**2 View Post
        C++(11) does *exactly* this:
        Code:
        template<typename T>
        T foo(const T x &, const int y) 
        {
            return x * y;
        }
        
        int main()
        {
            char  s[] = "text"; # 's' is text
            auto i = 0;      # 'i' is an int
            auto f = 0.0 ;   # 'f' is a float
        
            auto r = foo(s, i) ;# error: can't pass text as a first parameter
            auto r2 = foo(f, i) ;# works: because compiler can multiply a 'int' and a 'float'
            auto r3 = foo(i, i) ;# works: compiler can compile 'int' and 'int'
            return 0;
        }
        Yes, but my original point with that code was about how to design a language which was both powerful/efficient and fully strong-typed (like C++/D) and also simple to write & understand with a syntax not shy of weak-typed language (like Javascript/Python).

        It's of course possible in C++, and the same thing is much cleaner looking in D. However, it's still chalk full of all kinds of extra keywords and qualifiers. C++ gets much more complicated once you start adding in compile-time conditions, etc. D is better, but there's still a lot of it's syntax design which, IMO, could be improved (and never will, since it's strives to be compatible with C). I was responding to a comment about "the perfect language".

        Comment


        • #34
          Originally posted by log0 View Post
          That auto stuff is neat. I can't understand why it took them so long, should have been added to the spec together with the templates.
          As I understand, Bjarne Stroustrup has been fighting to get it in for over 20 years, but there were compatibility problems with C that were only worked out in time for C++11.

          Also, he's now fighting to get the auto keyword to be used as a form of template for the next revision:

          Code:
          auto foo(auto i, auto j) {
              return i * j;
          }
          Last edited by Vax456; 01-15-2013, 11:35 AM.

          Comment


          • #35
            Originally posted by F i L View Post
            I was responding to a comment about "the perfect language".
            An oxymoron if I ever heard one.

            Personally, I prefer C++ (though I admit I have yet to use D on a project of any size) for the majority of my work. Despite its occasional oddities, its simply the easiest to develop with (aside from C# for a Windows program) and easiest to expand.

            My least favorite language is the one I'm stuck with for the next decade: Ada, which comes in just ahead of Assembly on my list.

            Comment


            • #36
              Originally posted by newwen View Post
              It seems you're describing Vala.
              Does Vala work like that? I know it's variables are, by default, non-null types.. but I thought it was still classic ref-counting/smart-pointer stuff. My proposal would be more optimized (in theory) since 'var' memory would be directly tied to their scope, and all clean-up code (for both vars & refs) could be statically determined. Thereby avoiding the need to count references on variable assignment... now that I think about it though, I guess Vala could implement similar optimizations for their non-nullable types. Do you know if this is the case?

              There's also more things about Vala that I don't appreciate.. namely no operators or method overloads (also, I'm not really a fan of the overly-airy C#-like syntax, but it's not that bad). I talked to a Vala dev once, and he informed me that was due mostly to Vala's tie to GObject linking and the fact that Vala is OOC and designed to play perfectly with C.

              I would still like to know about any optimization information you're aware of though (or any links you might have on the topic).

              Comment


              • #37
                Originally posted by Vax456 View Post
                Also, he's now fighting to get the auto keyword to be used as a form of template for the next revision:

                Code:
                auto foo(auto i, auto j) {
                    return i * j;
                }
                OMG, me want this!!!

                Comment


                • #38
                  Syntax being easy to read, expressive and flexible seems like it's the most important thing.
                  (I notice that with the << it's easier to read, produces smaller code and is (less error prone?) shorter than printf.)
                  (The use of the %s, %d specifiers has an issue since the type specification seems to conflict with the DRY principle of software engineering. )

                  Comment


                  • #39
                    Originally posted by nslay View Post
                    The first one would never work in C. See stdarg.h for more information. In C++11, you can use variadic templates for a writeln() function as you described. But then, at the very worst, the compiler will produce a unique function for every output format you use. More likely, the compiler will inline such templated writeln() functions.
                    The first one was D, the second one was C.

                    Originally posted by Ancurio View Post
                    Also, as to "printf() vs cout", that just seems like such a stupid and unnecessary discussion.
                    You're not discussing languages, only conventions of standard libraries at best.
                    Yea, it was an example that people picked up. In general I just feel that C++ is too reliant on all their operators, so code is no longer as easily readable if you are not familiar with the meaning of each.

                    Originally posted by h**2 View Post
                    C++(11) does *exactly* this:
                    And for the reference, here's the D version (with an additional case and that is syntactically valid):
                    Code:
                    template foo(T, O : int) 
                    {
                        auto foo(T x, O y)
                        {
                        	return x * y;
                        }
                    }
                    
                    int main()
                    {
                        string  s = "text"; // 's' is text
                        auto i = 0;      // 'i' is an int
                        auto f = 0.0 ;   // 'f' is a double
                    
                        auto r = foo(s, i) ;// error: can't pass text as a first parameter
                        auto r2 = foo(f, i) ;// works: because compiler can multiply a 'int' and a 'float'
                        auto r3 = foo(i, i) ;// works: compiler can compile 'int' and 'int'
                        auto r4 = foo(i, f) ;// error: can't pass non-int as second parameter
                        return 0;
                    }

                    Comment


                    • #40
                      Originally posted by GreatEmerald View Post
                      The first one was D, the second one was C.



                      Yea, it was an example that people picked up. In general I just feel that C++ is too reliant on all their operators, so code is no longer as easily readable if you are not familiar with the meaning of each.
                      I think you're exaggerating C++'s reliance on operators since only two are used to define an extensible and cascaded input/output mechanism.

                      Function overloading and the cascading behavior of operators make a strong case for this scheme ... no matter how unusual it looks.

                      In the end, most C++ programmers don't write their own operators (except for extending stream operators). Those that do are often doing it for mathematical objects (where it makes sense) or string-like objects (where + and "concatenate" are universally synonymous).

                      Comment


                      • #41
                        Originally posted by GreatEmerald View Post
                        Well, even the very basic printing to console. C is consistent in that it uses a function for that, printf(). And C++ uses the "cout" notation that looks way out of place.
                        Well, the printing to console does not have anything to do with the language itself! You have to understand that C++ != STL and C != libc. Let me explain:
                        cout is actually an object and you just call operator "<<" on it. cout is not a keyword, it's just name for a struct that someone made and the "<<" operator was just simply chosen because it looks nicer than (+,-,*...). You don't have to use the STL library. It's absolutely not mandatory or recommended. (Embedded systems don't use STL, Qt doesn't use STL...)
                        Anyway, these operator overloading is a beautiful thing. You can do something like complexnum3 = complexnum1 + complexnum2 and you can define what + and = do. It's like automatic calling some function complexsum(c,a,b) without any overhead over ordinary function. The cout << is just like that. You can you use printf. You can write somehting new. You can even use inline assembly like in C.

                        Comment


                        • #42
                          Originally posted by GreatEmerald View Post
                          And for the reference, here's the D version (with an additional case and that is syntactically valid):
                          Code:
                          template foo(T, O : int) 
                          {
                              auto foo(T x, O y)
                              {
                              	return x * y;
                              }
                          }
                          
                          int main()
                          {
                              string  s = "text"; // 's' is text
                              auto i = 0;      // 'i' is an int
                              auto f = 0.0 ;   // 'f' is a double
                          
                              auto r = foo(s, i) ;// error: can't pass text as a first parameter
                              auto r2 = foo(f, i) ;// works: because compiler can multiply a 'int' and a 'float'
                              auto r3 = foo(i, i) ;// works: compiler can compile 'int' and 'int'
                              auto r4 = foo(i, f) ;// error: can't pass non-int as second parameter
                              return 0;
                          }
                          That D code can be even prettier:
                          Code:
                          auto foo(T)(T x, int y)
                          {
                              return x * y;
                          }
                          
                          void main()
                          {
                              auto s = "text"; // 's' is text
                              auto i = 0;      // 'i' is an int
                              auto f = 0.0 ;   // 'f' is a double
                          
                              auto r1 = foo(s, i); // error: can't pass text as a first parameter
                              auto r2 = foo(f, i); // works: because compiler can multiply a 'int' and a 'float'
                              auto r3 = foo(i, i); // works: compiler can compile 'int' and 'int'
                              auto r4 = foo(i, f); // error: can't pass non-int as second parameter
                          }

                          Comment


                          • #43
                            The choice of the word auto as a keyword for automatically determining type is an abomination in terms of expressiveness.
                            The word autotype should have been used.
                            (Auto is just too short and ambiguous. People are going to make mistakes easier.)

                            Also in re factoring projects where the word auto had another meaning it's now more of a problem.
                            You have to define an alias to know what has been re factored and what not. (Must see the difference easily remember, can't use things that rely on keywords being different.)

                            Comment


                            • #44
                              Originally posted by GreatEmerald View Post
                              How about this:
                              Code:
                              writeln("Today is January ", 15, ", ", 2013);
                              Or even:
                              Code:
                              printf("Today is January %d, %d\n", 15, 2013);
                              Really like the first one.
                              Would like to do the following:

                              Code:
                              uint intvariable = 5;
                              setWrite(std::cout);
                              
                              writeln("sghsfmhj",12," tyjumdnjmjdsgjhpg ", 2013," pesriprtu", intvariable);
                              write(" pegihjesj");
                              writeln(123);
                              writeln({5}133);
                              Would produce the following three lines of output:
                              Code:
                              sghsfmhj12 tyjumdnjmjdsgjhpg 2013 pesriprtu5
                               pegihjesj123
                                133
                              (The {} is used for format specifying but is in front or behind the object to be converted to a string.)
                              Also being able to do toString();
                              With comma separated stuff like in the example.
                              Also being able to do a toStringNl();
                              Same as the previous function but with an endline behind it.
                              Being able to use the << operator here:
                              <<("sfsgfsf ", 45," sdrghjdsgol");

                              (also use of {} with multiple parameters, Example: {6,"^"} Would reserve six places and have as second parameter a filler for the empty space, also multiple {} on one, cascading to form more interesting outputs)

                              Short, easy readable, see what you get out of it, concise, very functional and high-level.
                              Last edited by plonoma; 01-15-2013, 12:39 PM. Reason: removed character at wrong place

                              Comment


                              • #45
                                Originally posted by plonoma View Post
                                The choice of the word auto as a keyword for automatically determining type is an abomination in terms of expressiveness.
                                The word autotype should have been used.
                                (Auto is just too short and ambiguous. People are going to make mistakes easier.)

                                Also in re factoring projects where the word auto had another meaning it's now more of a problem.
                                You have to define an alias to know what has been re factored and what not. (Must see the difference easily remember, can't use things that rely on keywords being different.)
                                You mean too short and ambigous compared to short, long, double?

                                And if you find it too hard to substitute auto, you might be using the wrong source code editor.

                                Comment

                                Working...
                                X