5 ms·
As a bit of a type amateur, I wondered about this: why do we define primitive types and leave it at that? Why aren’t things like min and max first class constra
by Waterluvian 5y ago
As a bit of a type amateur, I wondered about this: why do we define primitive types and leave it at that? Why aren’t things like min and max first class constraints one can place on a type?
I imagine this basically gives you that. But do other languages do this? I’m guessing maybe functional programming languages?
- Vecr 5y agoAda does. It automatically selects the backing type depending on what range you specify.
- foobiekr 5y agoSince I encountered Ada in the late 80s, I've often wondered how this would work out over a long lived project, even when I was young and full of confidence and the expectation that long lived was going to be a thing of the path. C has been a headache in many ways, but one thing I've noticed is that there are many examples of very, very long-lived C projects, and not very many examples of very, very long-lived projects in most other languages (IBM, obviously, will disagree here, and COBOL). Some of that is related to C being a good fit for generating economic value at the beginning of the era that would now qualify as very long lived (20+ years, 30+ years), I'm sure, but it is interesting to consider that parts of some languages will not age well.
- touisteur 5y agoI've worked for 13 years on an Ada codebase initiated in the 90s. Ported from ppc to x86 then x86_64 to arm with mostly re-compilation and testing. Mind you, endianness was handled in a very ad-hoc (ugly but portable manner).
- ReleaseCandidat 5y ago> there are many examples of very, very long-lived C projects Of course there are, because that's the 'native' language for all Unix' and Unix-lookalikes and Windows because of the Kernels in C. So, for any OS relevant for 'real' computers of the last 30 years. Had the Lisp-Machines won the battle, nobody would talk about C any more (or just to scare children like with COBOL). And don't forget about Fortran, there is some _really_ ancient Fortran code in use at the same places that still use their COBOL.
- tinco 5y agoBecause it's hard and ultimately imperfect. Even a simple constraint as a max value (which inherently all integer values should have) requires the compiler to have good understanding of the code for it to recognize explicit bounds checks on the value. If there's no explicit bounds checks it would have to prove it through inference which is often (usually?) impossible even if the compiler was smart enough to do so.
- ReleaseCandidat 5y ago> [...] max value (which inherently all integer values should have) No, they shouldn't. Would really prevent many subtle bugs if the default integers of every language would be arbitrary precision ints.
- willvarfar 5y agoPython is an example language with “big integers”. Things quickly get interesting interoperating with other languages eg via json.
- ReleaseCandidat 5y agoDependent types is what you're searching for. Mostly used by (functional) proof 'languages' like Coq, Agda, Idris. For a language to have dependent types, the values that the types depend on must not (like with Rust) only be compile time constants, but values at runtime.
- tialaramex 5y agoHuh, surely the types in WUFFS are offering what the parent wanted (WUFFS won't let you add two 32-bit signed integers x and y together unless you've explicitly constrained them so that this operation won't overflow or underflow) but purely enforced at compile time since WUFFS wants to go real fast? WUFFS is in a related place to something like Coq. The idea in WUFFS is, if I write my decoder for the FOO image format in WUFFS, no matter how lazy and incompetent I am, your image displaying program can confidently use my FOO decoder, and if I did a bad enough job it might be slow or produce the wrong picture but it can't crash or run a SSH server or delete the password database or whatever.
- willvarfar 5y agoThis was common in Wirth languages eg Pascal, where they are called “sub-range types” https://wiki.lazarus.freepascal.org/subrange_types https://wiki.lazarus.freepascal.org/subrange_types It’s something I actually miss in modern mainstream languages. But as it’s something few copied and few lament, we can extrapolate that I’m a minority :) C99 has something similar but different in that integer fields in a struct can have a bit width specifier. This is is primarily used for bit fields in protocol frames and things, and conventional wisdom is that compilers generate poor code for them even today, and doing your own bit masks instead is still popular.
- auxym 5y agoNim's type system was inspired by Wirthian languagrs and it does have subranges.
- Animats 5y agoI wanted that in Ada, which has subranges. I wanted "ranges" instead - no mention of the underlying hardware at all. Intermediate values would be sized based on the rule that you can't overflow an intermediate value unless it would lead to overflow of the final sized value. Sometimes this requires longer intermediate values. What happened instead was standardized hardware. The 60 bit CPUs, the 48 bit CPUs, the 36 bit CPUs, the 24 bit CPUs, the 18 bit CPUs, and the 12 bit CPUs all died off. With everybody using powers of 2 of bytes, range calculation became less important for portability.
- smegcicle 5y agoLike what ReleaseCandidat said--- types originated as a compile-time thing, and things like range checks mostly make sense as run-time assertions, so it seems kind of strange from a compiler standpoint to conflate them... But, from the developer's standpoint, types are a very convenient place to put such run-time assertions. Raku has them.