Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
danielathome19 opened this issue Aug 20, 2022 · 3 comments
Open

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

danielathome19 opened this issue Aug 20, 2022 · 3 comments
Labels
leads question A question for the leads team long term Issues expected to take over 90 days to resolve.

Comments

@danielathome19
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

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
Copy link

github-actions bot commented Dec 6, 2022

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.

@github-actions github-actions bot added the inactive Issues and PRs which have been inactive for at least 90 days. label Dec 6, 2022
@jonmeow jonmeow added long term Issues expected to take over 90 days to resolve. leads question A question for the leads team and removed inactive Issues and PRs which have been inactive for at least 90 days. labels Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
leads question A question for the leads team long term Issues expected to take over 90 days to resolve.
Projects
None yet
Development

No branches or pull requests

4 participants