Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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,646
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.3k
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
  • 59.7k
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
  • 46.2k
45 votes

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

What is the usually method used when a compiler is type checking expressions with many operators and operands. Read wikipages on type system and type inference and on Hindley-Milner type system, ...
Basile Starynkevitch's user avatar
43 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
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
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
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
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
  • 27.5k
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
  • 34.1k
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,965
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
  • 59.7k
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

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,747
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

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
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.9k
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
  • 76.4k
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,442
12 votes

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

No, a test checking that a enum contains all the valid values and nothing more is essentially repeating the enum’s declaration. You would only be testing that the language properly implements the enum ...
jesm00's user avatar
  • 197
12 votes

Do any programming languages use types as values? Would there be any point?

Do any programming languages use types as values? Most languages that even have types (your question doesn't make sense for languages like ECMAScript, Python, Ruby, Smalltalk, Magpie, etc. which don'...
Jörg W Mittag's user avatar
11 votes

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

If your variables are floating-point numbers, IEEE754 (the floating point number standard which is supported by most modern processors and languages) has your back: it is a little-known feature, but ...
Federico Poloni's user avatar
11 votes

How are strings simultaneously objects and primitive data types in C#?

In C#, strings can be used like objects with methods, properties, and other features of objects. In C# strings are objects with methods and properties. At the same time, strings are treated the same ...
Eric Lippert's user avatar
  • 46.2k
10 votes

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

The quote is correct, but also really disingenuous. Let's break it down to see why: The wonderful thing about dynamic typing is it lets you express anything that is computable. Well, not quite. ...
jk.'s user avatar
  • 10.3k
10 votes

How are dynamic and static typings implemented?

That is generally correct. With static typing, up front optimizations are possible and it is not necessary to represent type information in memory at runtime. With dynamic typing, the relevant type ...
amon's user avatar
  • 134k

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