4 ms·
I had a strange internship topics involving Ada. I tried to run a huge program (a simple stdio wrapper around a huge library actually) on an Android machine (An
by aamederen 9y ago
I had a strange internship topics involving Ada. I tried to run a huge program (a simple stdio wrapper around a huge library actually) on an Android machine (Android 2.3, ARM). At that time, the only ADA compiler for ARM was available on BSD so I had used a Windows machine for development and a OpenBSD laptop for compiling the wrapper.
The subtyping feature I love the most. You see the benefit when you need to call a function which is taking many numeric arguments, all of them is a subtype so you can't give the arguments in the wrong order (latitude and longitude for example).
- gmac 9y agoFor this specific benefit, named parameters are arguably at least as useful, such as in Swift and Objective-C. [someObject doSomethingWithLatitude:55.0 longitude:0.0];
- krylon 9y ago> The subtyping feature I love the most. I only have very little experience with Ada, but being able to define a type whose values are integers in the range, say, 1 .. 7, to represent days of a week was an eye opener.
- dbrgn 9y agoRange types are probably the main thing I miss from the Rust type system. But even without range constraints, wrapper types (called newtypes in Haskell and Rust) are great. Let's say you have a function that accepts a temperature. A temperature is a float, but you want to prevent the user from mixing up celsius and fahrenheit. This is what newtype looks like in Rust: struct Celsius(pub f64); struct Fahrenheit(pub f64); fn print_celsius(temperature: Celsius) { println!("Temperature is {}", temperature.0); } It will fail to compile if you accidentally pass a Fahrenheit value to `print_celsius`, but at compile time all values are optimized down to plain floats, without any runtime cost. More programming languages should adopt this concept :)
- krylon 9y agoHuh. That's clever. This approach should also work in other languages, say, C/C++/C#. I don't remember if C uses structural identity for struct types, but it might work.
- jononor 9y agoNo identity or equality concept in C for structs apart from their address, I'm afraid. In C++ one can define a comparison operator, but it has to be implemented manually. Not that you can use the language-provided == on floats anyway, due to precision issues you should always check if the difference is below a specified limit...
- krylon 9y ago> Not that you can use the language-provided == on floats anyway, due to precision issues you should always check if the difference is below a specified limit... Hehe, I've been there. ;-) Fortunately, that code dealt with physical coordinates, so if two points were less than 100µm apart, they were equal for our purposes.
- johncolanduoni 9y agoOne of my favorite features of Scala that Java is missing is Scala's newtype-like functionality. It lets you define types which get erased to another one the JVM level (so there's no unnecessary boxing) but are checked in Scala and can hide their interior type and provide different methods.
- wBI8j2bsb 9y agoThis is exactly what C does. #include <stdio.h> typedef struct { double v; } Celsius; typedef struct { double v; } Fahrenheit; void print_celsius(Celsius temperature) { printf("Temperature is %f", temperature.v); } void compile_error(Fahrenheit temperature) { print_celsius(temperature); } I suspect LLVM will clean this up to plain floats as well.
- 9y ago
- oblio 9y agoI find this regression kind of strange. Pascal had number ranges in the 80s! I don't get why no modern language adopted them...
- deleted 9y ago[deleted]
- kqr 9y agoThough if you're dealing in days of the week, you probably want an enumeration to avoid the whole "does 1 mean Sunday" thing.
- AstralStorm 9y agoWhich is why Ada has a Day type, an enumeration.
- kqr 9y agoImportant terminology nit: subtypes and derived types are two different things in Ada. A "subtype" is essentially a local new name for an existing type (it makes little sense to export subtypes). A derived type is a new type that is based on another type, but can be restricted to a subset of values of the original type, and cannot be interchanged with the original type.