Let's say, we create a reimplementation of C, with the only difference being that types are inferred. Storage classes and modifiers would still need to be given (const, static, restrict etc), and let's restrict our attention to single file C programs for the moment. Could it be done? What are the major impediments?
Some thoughts on what might cause problems with type inference
casts would probably need a "from" annotation, something like
var i = (uint32_t -> uint64_t) *some_pointer;
These problems would require a bit of user annotation, but shouldn't be too burdensome, is there some killer issue that blows this idea out of the water?
Edit: To clarify, I'm not talking about adding generics or parametric polymorphism, just type inference for existing C types.
Edit 2014: Anyone interested in this concept may want to look into Rust
When we declare a variable in C, it is necessary that a type is explicitly associated to it (e.g., int or double ). This is a distinction to languages that feature a so-called type inference, where programmers are freed from the need of annotating types.
Some languages that include type inference include C++11, C# (starting with version 3.0), Chapel, Clean, Crystal, D, F#, FreeBASIC, Go, Haskell, Java (starting with version 10), Julia, Kotlin, ML, Nim, OCaml, Opa, Q#, RPython, Rust, Scala, Swift, TypeScript, Vala, Dart, and Visual Basic (starting with version 9.0).
Type inference is the automatic deduction of the data types of specific expressions in a programming language, usually done at compile time.
Python doesn't do static type inference, because it wants to let you do things that are impossible under such a scheme.
GCC 5.1 supports:
__auto_type
extension, analogous C++11 auto
typeof
extension, analogous to C++11 decltype
/* Same as: double j = 0.5; */
typeof(1 + 0.5) j = 0.5;
assert(j == 0.5);
Linux kernel example: How does the typecheck macro from the Linux kernel work?
_Generic
C11 keyword: Syntax and Sample Usage of _Generic in C11
__auto_type i = 1;
assert(_Generic((i), int: 1, default: 0));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With