Skip to main content
109 votes

How do variables in C++ store their type?

Variables (or more generally: “objects” in the sense of C) do not store their type at runtime. As far as machine code is concerned, there is only untyped memory. Instead, the operations on this data ...
amon's user avatar
  • 134k
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
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
54 votes

How do variables in C++ store their type?

The other answer explains well the technical aspect, but I'd like to add some general "how to think about machine code". The machine code after the compilation is pretty dumb, and it really just ...
Frax's user avatar
  • 1,844
39 votes

Does a `long` ban make sense?

No, banning the builtin integer types would be absurd. They should not be abused either, however. If you need an integer that is exactly N bits wide, use std::intN_t (or std::uintN_t if you need an ...
5gon12eder's user avatar
  • 7,196
36 votes
Accepted

Use empty string, null or remove empty property in API request/response

TLDR; Remove null properties The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if programming in that paradigm). The JSON that you receive is ...
Kasey Speakman's user avatar
24 votes

What do relational databases gain by setting a predefined data type for each column?

First: plain text is binary (it's not even the UTF8 or ASCII characters "0" and "1" but actual on/off bits) That said, some of the reasons are: Business/design constraints: allowing the number ...
Tulains Córdova's user avatar
22 votes

Is static typing worth the trade-offs?

There are a lot of strong opinions surrounding the debate but obviously this isn’t actually a matter of opinion, it’s a matter of facts. So we should look at empirical research. And the evidence from ...
21 votes

Use empty string, null or remove empty property in API request/response

Going with an empty string is a definitive no. Empty string still is a value, it is just empty. No value should be indicated using a construct which represents nothing, null. From API developer's ...
Andy's user avatar
  • 10.3k
19 votes
Accepted

Does a `long` ban make sense?

The only reason I would use long today is when calling or implementing an external interface that uses it. As you say in your post short and int have reasonably stable characteristics across all ...
Peter Green's user avatar
  • 2,252
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
Accepted

Why does Java have primitives for different size numbers?

Like so many aspects of language design, it comes to a trade-off of elegance against performance (not to mention some historical influence from earlier languages). Alternatives It is certainly ...
gardenhead's user avatar
  • 4,757
16 votes
Accepted

Subtyping without adding state or behavior - Bad Practice?

Java's exception handing mechanism uses the class hierarchy to model a taxonomy of exception cases. Catch phrases effectively do instanceof tests. Thus they can distinguish ClosedByInterruptException ...
Jerry101's user avatar
  • 5,407
14 votes

Can an interpreted statically typed language be considered type safe?

Statically typed language: a language that does not allow you to change the type of a variable at run-time. That's not the definition of statically typed. Statically typed means that type checking (...
Jörg W Mittag's user avatar
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
  • 75.9k
13 votes
Accepted

Why isn't there a primitive "complex number" type in Java?

In general, it is a bad sign if you need to change the language to add a new datatype or operation. That means that the language is lacking some feature, preventing you from expressing your program. ...
Jörg W Mittag's user avatar
13 votes
Accepted

What should be the datatype of the tokens a lexer returns to its parser?

Generally, if you're processing a language though lexing and parsing, you've got a definition of your lexical tokens, e.g.: NUMBER ::= [0-9]+ ID ::= [a-Z]+, except for keywords IF ::= 'if' ...
Joshua Taylor's user avatar
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
Accepted

Only one number type in language design

We have different number representation in general because they have different strengths and weaknesses, be it speed, precision, or range. Also this has to be the case because we cannot represent all ...
jk.'s user avatar
  • 10.2k
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
10 votes
Accepted

Why and when would you use a .NET framework data type?

These are not two distinct, different types - System.String and string are not equivalent types, they are simply two aliases to the same type. The choice of using one or the other is thus entirely ...
Avner Shahar-Kashtan's user avatar
10 votes

Why does Java have primitives for different size numbers?

The reason is very simple: efficiency. In multiple ways. Native data types: The closer the data types of a language match the underlying data types of the hardware, the more efficient the language ...
Mike Nakis's user avatar
  • 32.2k
10 votes

Why are Python sets and dictionaries not ordered by default?

You are correct that item are store internally with some order, but this internal order is determined by the hash code of the key, which is what allows retrieval to be so fast. So if a set/dict should ...
JacquesB's user avatar
  • 59.6k
10 votes

How to Represent Functional Boolean

The "functional version" of booleans is the Church Encoding of Booleans in the λ-calculus. In λ-calculus, the only abstraction mechanism that exists, are (anonymous) functions. So, naturally,...
Jörg W Mittag's user avatar
9 votes

Why are Python sets and dictionaries not ordered by default?

Your premise is incorrect. As of Python 3.6, dicts remember their insertion order. This was an implementation detail, and was promoted to full language feature in 3.7. In 3.6, for the specific case ...
Kevin's user avatar
  • 2,658
8 votes

What does the "t" in int32_t signify?

At the time the C99 Standard was ratified, there already existed countless C programs that used int32 as an identifier. On platforms where both int and long were 32 bits, some of that pre-existing ...
supercat's user avatar
  • 8,443
8 votes

What should be the datatype of the tokens a lexer returns to its parser?

As said in the title, which data type should a lexer return/give the parser? "Token", obviously. A lexer produces a stream of tokens, so it should return a stream of tokens. He mentioned Flex, a ...
Eric Lippert's user avatar
  • 46.2k
8 votes

What do relational databases gain by setting a predefined data type for each column?

It is so that the underlying code that the database is written in can allocate and use fixed size records, if it knows that a specific field can contain 0 to 256 characters of text then it can ...
Steve Barnes's user avatar
  • 5,310

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