Skip to main content
16 events
when toggle format what by license comment
Mar 23, 2016 at 11:33 comment added Zaibis @JörgWMittag: Ok, digging up an old post of myself, I realized that this is supported in C++ since c++11. stackoverflow.com/q/34762708/2003898 But you should any way note in that case that just the altest versions do so.
Mar 23, 2016 at 11:26 comment added Zaibis I know the spec pretty well. What you're refering to is called "auto int".This just assumes: Using the auto storage specifier (which is by the way pretty obsolete and without any usage) without declaring a type the compiler has to assume it is meant to be integer. So writing auto i = 1; is doing the same as writing auto int i = 1 which is as far as I understand not the same as type inference. I'm not sure how familiar you are with the C++ specs but I dare to assume you are missunderstanding what auto is actually doing. but you examples are no infering but just implicite declarations.
Mar 23, 2016 at 10:41 comment added Jörg W Mittag @Zaibis: auto i = 1 // i is inferred to type int, vector<int> vec; auto itr = vec.iterator(); // itr is inferred to type vector<int>::iterator and so on. If you want to know how exactly that works, you can look it up in the spec.
Mar 23, 2016 at 10:36 comment added Zaibis I'm really currious how you concider C++ supporting type infereing. Unless by tiype inference you mean that there are multiple type names for the same byte size. But especially C++ even requires memory allocations to be explicitely casted and when ever you do an ambiguous asignment it is at least warned about and if there are ambiguousitys on equal ranking it is even a error. So while you could be correct (I absolutely doubt!) I would be pretty much interested in how it works to leave explicit types away in C++ where everything depends on these distinct types and its informations
Mar 22, 2016 at 13:39 comment added jfs JörgWMittag: as @KRyan already said, the answer "you don't have to" is not very interesting (it is trivially obvious -- many languages allow to omit the type declarations in some cases). The question "why would you want to declare types" is much more interesting and better reflect the spirit of the original question (your answer reminds me of the joke: "where we are?" -- "you are in a hot-air balloon!"). You don't need to know what a designer of a specific language thought at the time, to provide some good reasons in favor of type declaration.
Mar 22, 2016 at 8:01 comment added Jörg W Mittag @J.F.Sebastian: The question is not "why may I declare types", but "why do I have to declare types", and in Python, you don't have to declare types. Also, the question is about types of variables, and Python does not support type annotations of variables, only functions. And lastly, while type annotation syntax is part of the language, types aren't – they are provided by third-party libraries.
Mar 22, 2016 at 5:30 comment added jfs Even in dynamic languages you may declare types e.g., Python has type hinting
Mar 22, 2016 at 5:05 comment added Schalat The reasons I write types are: a) some types cannot be inferred and b) machine-checked documentation. For an example, polymorphic type inference is impossible for Haskell.
Mar 21, 2016 at 21:22 comment added KRyan @JörgWMittag Yes, I think some inclusion of the idea that explicitly specifying type has benefits, including some examples, would dramatically improve this answer. It does not need to reprint a book, though a reference to a book for more reading is good, but it should at least touch upon the topic.
Mar 21, 2016 at 21:20 comment added Jörg W Mittag @KRyan: if you want to know why a certain language designer made a certain language design choice, you will have to ask that language designer, I'm afraid. I cannot tell you why the designers of C♯ decided against type inference, nor can I tell you why they later changed their minds. Language design is heavily opinionated and often comes down to taste. If, OTOH, you want to know about the specific trade-offs involved, the answer would basically be a re-print of Prof. Pierce's Types and Programming Languages which is much too broad for Stack Exchange.
Mar 21, 2016 at 20:55 comment added KRyan This is great info about misconceptions behind the question, but still leaves the question unanswered. The question, more properly, is why do we have to specify the data type when declaring variables in languages that require that? Why were they designed way? There are good answers to that question, and while elaborating on alternatives broadens the OP's horizons and is very good, that doesn't seem complete to me.
Mar 21, 2016 at 18:19 comment added Dan Lyons Regarding type inference, many languages, including ML-based languages, use Hindley-Milner. Lambda calculus is magic.
Mar 21, 2016 at 10:55 comment added dcorking Plus one for explaining both type inference and dynamic typing (late binding)
S Mar 21, 2016 at 10:25 history suggested Racheet CC BY-SA 3.0
fixed a confusing phrasing.
Mar 21, 2016 at 10:23 review Suggested edits
S Mar 21, 2016 at 10:25
Mar 21, 2016 at 0:52 history answered Jörg W Mittag CC BY-SA 3.0