5 ms·
Type-inference is needed in C++ (and is already part of the next standard, you can enable the auto keyword with gnu gcc already). But it's needed for entirely d
by thras 17y ago
Type-inference is needed in C++ (and is already part of the next standard, you can enable the auto keyword with gnu gcc already). But it's needed for entirely different purposes than what scripting languages use it for: it's a feature for writing better templates.
It's confusing because there are two types of type-inference (run-time and compile-time). You do lose performance with run-time type-inference, which is what most interpreted languages use. C++ already has compile-time type-inference in the form of templates. They are...complex. But they're also faster than anything similar. There's a reason that C++'s sort is usually faster than C's qsort for complex types.
C++ doesn't have run-time type-inference and probably won't. The performance loss is too big to build it into the language everywhere, and unless you do that it's pretty pointless. You can tack it on with boost::any or RTTI, for example, but you'll generally find that it's not the correct decision from an engineering standpoint. Scripting languages have run-time type-inference built in everywhere -- but they do it at the cost of performance.
Now, as far as the problems of C++'s dependence on C...well, you're right. It's a feature of C++'s history. If someone could come along and invest the millions of man hours required in making a performance-critical high-level language without the C-baggage (and then market it!), it would be a great thing. But that's a pipe dream. The best we're going to do on that front is Java.
- loup-vaillant 17y agoErr, by "runtime type inference", you actually mean runtime type checking, right? The pipe dream you are talking about has a tiny hope of coming true here: http://www.bitc-lang.org/ http://www.bitc-lang.org/ EDIT: replying to thras, below. According to the very Wikipedia article you cited, there is no such thing as "runtime type inference". What you described as such is a way of implementing dynamic type checking: checking at runtime that the types of the various parts of an expression actually match. You should actually read your sources. So, "type checking" isn't always performed at compile time.
- thras 17y agoHmm. Check this out: http://en.wikipedia.org/wiki/Type_inference http://en.wikipedia.org/wiki/Type_inference Type checking is a little different, and refers to some safety measures done at compile time that theoretically guard against certain types of errors. Scripting languages don't do them, and don't miss out on much programming correctness as far as I can tell. Full run time type inference in C++ is about having a container of something -- let's say void pointers that just point to memory locations, and figuring out what sort of thing they're pointing at. Or similarly, you have a base-class pointer and want to figure out which derived version of the object is being pointed at.
- lincolnq 17y agoI replied to you above, but I'll reply to your specific example here. Your example is really not how PL people talk about type inference. However, you're right that you wouldn't call this example type-checking either... it would probably be a fuzzier term such as "reflection" or "introspection". But certainly not type inference. Do you understand what the wikipedia article you linked is about? Because it's NOT about introspecting the type of a contained object within a container. It's about statically making proofs of the type of a variable based on its usage.
- thras 17y agoSorry, but people use the term this way: http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=qrP&q=%22runtime+type+inference%22&aq=f&oq=&aqi= http://www.google.com/search?hl=en&client=firefox-a&...
- loup-vaillant 17y agoA quick read of this page and its top link strongly suggest that runtime type inference is an optimization designed to speed up the interpretation of dynamically typed programs. It sounds very useful for JIT compilation (to make appropriate code specializations). It's also likely too complex to be implemented in simple interpreters (like Lua's). Note that this term isn't very widely used: we are already in second position at your link. So, unlike ML-style compile-time type inference, runtime type inference is implementation specific. Your earlier statement "Scripting languages have run-time type-inference built in everywhere" is actually ill-typed. However, if you had said "checking" instead of "inference", your sentence would have been correct. Hence my "Err, by "runtime type inference", you actually mean runtime type checking, right?"
- thras 17y agoType checking is what you say when the compiler makes sure that you've used all your types correctly, and spits out an error when you don't. It's there to prevent you from trying to assign an int to a string, for example. "Runtime type checking" is the same thing at runtime. Errors and exceptions get thrown, your program stops. So no, that's not what we're talking about. Here is a description of type checking: http://en.wikipedia.org/wiki/Type_system#Type_checking http://en.wikipedia.org/wiki/Type_system#Type_checking
- thras 17y agoAccording to the very Wikipedia article you cited, there is no such thing as "runtime type inference" I can't seem to find that statement anywhere in the article. And it hasn't been edited since September. Also, why not google for "runtime type inference": http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=YhL&q=runtime+type+inference&aq=f&oq=&aqi= http://www.google.com/search?hl=en&client=firefox-a&...
- lincolnq 17y agoThere is such a thing, but it has a different meaning than how I believe you used it. You said "scripting languages have run-time type inference built in everywhere". I don't think that's true for most scripting languages. Inference refers to determining facts which were not explicitly provided, and usually implies a statically typed programming language where inference is done at compilation time. "Runtime type inference" might be used for, e.g., Python's Psyco project, where the just-in-time compiler infers properties about a particular variable (such that it's always an integer) and can therefore compile out boxing, unboxing, runtime type checks and so on. Speaking of runtime type-checking, I inferred that that's what you meant when you said "runtime type inference". Type-checking IS something that happens at runtime almost everywhere in dynamic languages. It is also more closely associated with RTTI -- a dynamic_cast in C++ would not be termed type inference (the previous and new types are known), but a type check does occur at runtime.
- derefr 17y agoBy "type inference," I just meant "that clever stuff Haskell does." :)
- toadpipe 17y agoIf someone could come along and invest the millions of man hours required in making a performance-critical high-level language without the C-baggage As long as it also had C syntax, and something close to the C memory model, and was recognizably object oriented and/or functional. And came with a lot of libraries. And 3D graphics engines. And physics engines. Unless some independent game developer does something in a new language that other developers can't easily duplicate with their current ecosystem (not that it couldn't be done in C++), and it catches on, there's no incentive to do anything other than continue to evolve things in the most backwards compatible way. There are always more C++ programmers coming off the assembly lines who want nothing more than to work on games.