carbon-language / carbon-lang

Carbon Language's main repository: documents, design, implementation, and related tools. (NOTE: Carbon Language is experimental; see README)
http://docs.carbon-lang.dev/
Other
32.25k stars 1.47k forks source link

Proposal: Add a `dynamic` and/or `void` type #2076

Open danielathome19 opened 2 years ago

danielathome19 commented 2 years ago

Dynamic typing is extremely powerful for data science and machine learning. Since this language will have a competitive performance to C++, it makes sense to me that future ML libraries may be written in Carbon for use in Python or standalone, especially for an embedded model akin to TF lite. Dynamic (or even duck typing, at minimum) is very helpful for rapidly writing generic code, such as for data structures, scientific models, and databases such as OLAP cubes.

C# performs this using the dynamic data type, as well as in Rust, allowing for the same type-flexibility as Python without the performance decrease from runtime-interpreted type inference. This is also easily done in C/C++ using the void* type pointer:

#include<stdlib.h>
int main() {
   int a = 7;
   float b = 7.6;
   void *p;
   p = &a;
   printf("Integer variable is = %d", *( (int*) p) );
   p = &b;
   printf("\nFloat variable is = %f", *( (float*) p) );
   return 0;
}

C++ also has the variant type which is similar, D has jsvar to enable JS-like dynamic typing, and Golang's interface type is nearly flexible enough almost to make it useable, falling just short.

In Carbon, this could look something like:

fn Main() -> void {
  var x: dynamic = 5;
  x = 3.1415;
  Print(x);
}

This would be great for working with embedded systems and interop with C/C++, and it's something I use on a near-daily basis, personally.

OlaFosheimGrostad commented 2 years ago

Carbon most certainly will have a tagged union like std::variant. One can hope that Carbon also will be able to come up with a safe and flexible take on untagged unions (#1849), even though that is quite demanding in terms of type-checking.

For the most part I would expect to Carbon to go for static typing as that is a weakness in C++ that Carbon ought to address. So I am not sure if Carbon would be the most convenient language to use if you prefer dynamic typing as a programming paradigm.

geoffromer commented 2 years ago
fn Main() -> void {
  var x: dynamic = 5;
  x = 3.1415;
  Print(x);
}

We do plan to support dynamic typing, but probably not in quite this form. In particular, in order for code like this to work, I expect that you will need to either explicitly down-cast x in the Print call (as in your void* example), or constrain x in its declaration so that it can only hold values of types that can be passed to Print (using something like Rust's dyn).

github-actions[bot] commented 1 year ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive label. The long term label can also be added for issues which are expected to take time. This issue is labeled inactive because the last activity was over 90 days ago.