Open danielathome19 opened 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.
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
).
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.
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 thevoid*
type pointer:C++ also has the
variant
type which is similar, D hasjsvar
to enable JS-like dynamic typing, and Golang'sinterface
type is nearly flexible enough almost to make it useable, falling just short.In Carbon, this could look something like:
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.