Hot answers tagged

18

Think of a project with many programmers, that has changed over the years. You have to maintain this. There's a function getAnswer(v) { return v.answer } What on earth does it do? What's v? Where does the element answer come from? getAnswer(v : AnswerBot) { return v.answer } Now we have some more info —; it needs a type of AnswerBot. If we go ...


17

More over, with REPLs it's trivial to test a function for it's return type with different inputs It's not trivial. It's not trivial at all. It's only trivial to do this for trivial functions. For instance, you could trivially define a function where the return type depends entirely on the input type. getAnswer(v) { return v.answer } In this case, ...


8

First of all, it seems to me you are missing (or perhaps misunderstanding) the "explicitly" bit in "without being explicitly programmed" (from the quote in the question). It doesn't mean that no programming is required at all, it means that you are not programming a specific solution to the problem, but instead what you are making is a more general program ...


8

In 1963, Tony Hoare proposed adding implicit type rules to ALGOL. The ALGOL committee boxed his ears, HARD. Requiring variables to be declared explicitly was known, EVEN THEN, shown to reduce errors in programming. Tony mentioned this in his Turing talk, and said it was BEFORE the probably-apocryphal Venus probe FORTRAN story, where a typo in a FORTRAN DO-...


8

You seem to have a few misconceptions about working with large static projects that may be clouding your judgement. Here are some pointers: even if you declare the return type of a function, you can and will forget it after you've written many lines of code, and you will still have to return to the line in which it's declared using the search function ...


7

Computers use Binary Code. Binary is used to encode both instructions and data. This is because transistors store and manipulate binary very nicely. It turns out that with enough boolean operations, you can perform the mathematical operations of addition, subtraction, multiplication, division, etc... It is possible to use a base other than binary, but ...


7

Ultimately, the answer is that the concept of unsigned floating point numbers is not particularly useful. Concepts that are extremely useful make their way into the standards. Concepts that are marginally useful might make their way into add-on packages. The concept of an unsigned float doesn't even fit into the marginally useful concept. It throws out far ...


6

What is the connection between type inference and advanced type systems? There are three things off the top of my head: Language designers interested in type systems will focus their language design around them. This means that all sorts of type oriented features were considered/implemented - inference being one. ML had it, so when follow up languages ...


6

Because static checkers are easier for statically typed languages. At a bare minimum, with no dynamic language features, if it compiles, then at runtime there are no unresolved functions. This is common in ADA projects and C on microcontrollers. (Microcontroller programs get big sometimes... like hundreds of kloc big.) Static compile reference checks are ...


5

When Java encounters this + operator, it compiles the addition to byte code, specifically an iadd instruction. What the computer does when executing this instruction depends on how the virtual machine on which this program is executed was programmed. It might delegate to an ADD opcode from some processor's instruction set. It might consult an internal ...


5

I don't believe such a possibility was ever considered by the C++ committee. Further, although FORTRAN did have a sort-of implicit type system (variables starting with 'I' through 'N' were integers, everything else was real) I can't quite see how such a thing would work in a block-structured language like C++. For example, consider code like this: int f() {...


5

Allowing optional types where they used to be required can make the syntax ambiguous. It is easier to add a keyword (or to reuse one in this case) than changing the grammar.


4

The C++ syntax is designed in such a way that a declaration always needs a type to distinguish it from assignment. You simply cannot leave out the type, because that already has a different meaning: int i = 1; // Leaving out the type turns it into assignment: i = 1; int i; // Leaving out the type turns it into evaluation: i; You simply need an explicit ...


4

I never understood statements like this one. To be honest, even if you declare the return type of a function, you can and will forget it after you've written many lines of code, and you will still have to return to the line in which it's declared using the search function of your text editor to check it. It's not about you forgetting the return type -- ...


3

You remember the old adage "garbage in, garbage out", well, this is what static typing helps to prevent. Its not a universal panacea but the strictness over what kind of data a routine accepts and returns means that you have some assurance that you're working correctly with it. So a getAnswer routine that returns an integer will not be useful when you try ...


1

Unsigned ints are mostly a performance optimization desperately trying to save even one bit of memory. Floats, on the other hand, weren't viable anyway for such systems, so there's less pressure on them to make such tiny savings. Their range is already huge anyway. The implementation of unsigned integers in modern languages is more about compatibility than ...


1

It always boils down to address space. In a modern computer, any address space is a power of 2. E.g. you can fit 256 different values in a byte (2^8), 65536 in a 2-byte word (2^16). Directly related to that, memory sizes only come in powers of 2. Expressing these values as decimal does not "fit" the address space, meaning you do not run out of digits at the ...


1

Would that be considered as a bad practice/idea? Enh? My first instinct is that you wouldn't gain much in performance. Spinning up a new process and sending in the data is a bit heavyweight. My second instinct is I'm not sure how general you're going to be able to do it. Just because methods return no data doesn't mean they don't work with data. You're ...


1

In just about any programming language, you can pass some type of list, array, tuple, record, or object as the only argument. It's only purpose is to hold other items instead of passing them to a function individually. Some Java IDE's even have an "Extract Parameter Object" feature to do just that. Internally, Java implements variable numbers of arguments ...


1

A singly-linked list is the simplest persistent data structure. Persistent data structures are essential for performant, purely functional programming.


1

In computer science, a function or expression is said to have a side effect if it modifies some state or has an observable interaction with calling functions or the outside world. From Wikipedia - Side Effect A function, in the mathematical sense, is a mapping from input to output. The intended effect of calling a function is for it to map the input ...



Only top voted, non community-wiki answers of a minimum length are eligible