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 ...
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 "...
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 ...
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 ...
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.
...
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, ...
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 ...
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,...
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 (...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 = ...
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 ...
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 ...
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 ...
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 ...
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 ...
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,...
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 ...
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-(...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
type-systems × 169programming-languages × 26
haskell × 21
static-typing × 19
java × 14
functional-programming × 14
data-types × 13
object-oriented × 12
dynamic-typing × 12
generics × 11
c# × 8
c++ × 8
python × 8
language-design × 8
compiler × 7
interfaces × 6
inheritance × 6
scala × 6
typescript × 6
type-safety × 6
design-patterns × 5
terminology × 5
polymorphism × 5
weak-typing × 5
design × 4