140 votes
Accepted

Why exactly does Java not allow numeric conditionals like if(5) { ...} if C does?

1. C and Java are different languages The fact that they behave differently should not be terribly surprising. 2. C is not doing any conversion from int to bool How could it? C didn't even have a true ...
8bittree's user avatar
  • 5,656
85 votes
Accepted

How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?

The common way to do this, at least with functional languages is to use a discriminated union. This is then a value that is one of a valid int, a value that denotes "missing" or a value that denotes "...
David Arno's user avatar
  • 39.1k
83 votes

Why do mainstream strong static OOP languages prevent inheriting primitives?

I assume you are thinking of languages like Java and C#? In those languages primitives (like int) are basically a compromise for performance. They don't support all features of objects, but they are ...
JacquesB's user avatar
  • 57.8k
58 votes

How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?

If you do not already know what a monad is, today would be a great day to learn. I have a gentle introduction for OO programmers here: https://ericlippert.com/2013/02/21/monads-part-one/ Your ...
Eric Lippert's user avatar
  • 45.9k
42 votes
Accepted

Should one test the values of an enum using unit tests?

If you have an enum with values only (no methods as one could do in Java), and this enum is part of the business definition of the system, should one write unit tests for it? No, they are just state. ...
VoiceOfUnreason's user avatar
41 votes

Are there any design patterns that are possible only in dynamically typed languages like Python?

Short answer: no, because Turing equivalence. Long answer: This guy's being a troll. While it's true that type systems "restrict you to a subset," the stuff outside that subset is, by definition, ...
Mason Wheeler's user avatar
35 votes

How do the type systems in functional languages differ from those in OO languages?

tl;dr summary: they are more powerful, more expressive, and most importantly, they don't lie. [Aside: I would like to point out that there is a double false dichotomy in the OP's question. FP and OO ...
Jörg W Mittag's user avatar
34 votes

Using a "strong" type system in the real world, say, for large-scale web-apps?

I'll give a short answer due to my lack of time at the moment, but I'm currently working on two big projects (> 100.000 LOC in Haskell) - flowbox.io and luna-lang.org. We use Haskell for all the parts,...
danilo2's user avatar
  • 669
34 votes
Accepted

Could Hindley-Milner inference work for the Go language?

The Hindley-Milner type inference is used for Hindley-Milner type systems, a restriction of System-F type systems. The interesting feature of HM type systems is that they have parametric polymorphism (...
amon's user avatar
  • 134k
34 votes
Accepted

What is a type system?

That all seems like a fine description of what type systems provide. And your implementation sounds like a reasonable enough one for what it's doing. For some languages, you won't need the runtime ...
Telastyn's user avatar
  • 109k
28 votes

Are there any design patterns that are possible only in dynamically typed languages like Python?

I'm going to side-step the 'pattern' part because I think it devolves into the definition of what is or isn't a pattern and I've long lost interest in that debate. What I will say is that there are ...
JimmyJames's user avatar
  • 25.9k
24 votes
Accepted

Why is behavorial subtyping undecidable?

Let the contract of operation o of Type T be that it halts for all inputs. Now decide whether operation o of subtype S <: T satisfies that contract: you have just solved the Halting Problem. More ...
Jörg W Mittag's user avatar
23 votes

What is a type system?

I like @Telastyn's answer especially because of the reference to academic interest in formalism. Allow me to add to the discussion. What is a type system? A type system is a mechanism for ...
Erik Eidt's user avatar
  • 33.6k
21 votes
Accepted

Naming of union and intersection types in TypeScript

However, is the resulting type of the | not actually an intersection, and that of & a union? No. These concepts come from functional programming and type theory, so to understand them, it helps ...
Filip Milovanović's user avatar
20 votes

Why do mainstream strong static OOP languages prevent inheriting primitives?

What some language propose is not subclassing, but subtyping. For example, Ada lets you create derived types or subtypes. The Ada Programming/Type System section is worth reading to understand all ...
coredump's user avatar
  • 5,925
20 votes

Should one test the values of an enum using unit tests?

You don't test an enum declaration. You may test whether function input/output has the expected enum values. Example: enum Parity { Even, Odd } Parity GetParity(int x) { ... } You don't ...
JacquesB's user avatar
  • 57.8k
20 votes
Accepted

Is there a practical use for dependent types?

I think what you might be missing is the value you are depending on doesn't have to be a constant like 3. The length is often specified generically, so you may not know the exact length, but you can ...
Karl Bielefeldt's user avatar
18 votes

Why are semantics and type systems are so important?

Sublte semantic differences Consider this simple program: i = 1000'000'000 j = i * i This operation can behave very differently depending on the semantics of integers in your language: Some ...
CodesInChaos's user avatar
  • 5,697
18 votes

How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?

I think that in this case a variation on a Null Object Pattern would be useful: public class Measurement { private int value; private bool isUnknown = false; private bool isMissing = ...
Maciej Stachowski's user avatar
17 votes

Using a "strong" type system in the real world, say, for large-scale web-apps?

Well, weak vs. strong typing is pretty vaguely defined. Further, since closest there is to a general use of 'strong typing' is to refer things that make it difficult to cast types, that leaves nothing ...
Steven Armstrong's user avatar
17 votes

Why do mainstream strong static OOP languages prevent inheriting primitives?

I think this might very well be an X/Y question. Salient points, from the question... My motivation is maximising the use of type system for compile-time correctness verification. ...and from your ...
underscore_d's user avatar
17 votes

What is a type system?

Oh man, I am excited to try to answer this question as best I can. I hope I can get my thoughts properly in order. As @Doval mentioned and the questioner pointed out (albeit rudely), you do not ...
gardenhead's user avatar
  • 4,729
16 votes
Accepted

What is the common procedure used when compilers statically type check "complex" expressions?

Recursion is the answer, but you descend into each subtree before handling the operation: int a = 1 + 2 - 3 * 4 - 5 to tree form: (assign (a) (sub (sub (add (1) (2)) (mul (3) (4))) (5)) Inferring ...
Simon Richter's user avatar
15 votes

Why exactly does Java not allow numeric conditionals like if(5) { ...} if C does?

C 2011 Online Draft 6.8.4.1 The if statement Constraints 1    The controlling expression of an if statement shall have scalar type. Semantics 2    In both ...
John Bode's user avatar
  • 10.8k
14 votes

How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?

If you literally MUST use an integer then there is only one possible solution. Use some of the possible values as 'magic numbers' that mean 'missing' and 'unknown' eg 2,147,483,647 and 2,147,483,...
Ewan's user avatar
  • 72.4k
13 votes

What does weak static typing/strong dynamic typing mean?

The terms strongly typed and weakly typed dont have a agreed-upon definition. Therefore, unless you define what you mean by "strongly typed" and "weakly typed", it is impossible to answer your ...
Jörg W Mittag's user avatar
13 votes

What is the common procedure used when compilers statically type check "complex" expressions?

In C (and frankly most statically typed languages based on C) every operator can be seen as syntactic sugar for a function call. So your expression can be rewritten as: int a{operator-(operator-(...
ratchet freak's user avatar
13 votes

Should one test the values of an enum using unit tests?

If there's a risk that changing the enum will break your code then sure, anything with the [Flags] attribute in C# would be a good case because adding a value between 2 and 4 (3) would be a bitwise 1 ...
Ian's user avatar
  • 5,462

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