sharkdp / numbat

A statically typed programming language for scientific computations with first class support for physical dimensions and units
https://numbat.dev
Apache License 2.0
1.14k stars 47 forks source link

Generic structs #452

Open sharkdp opened 3 months ago

sharkdp commented 3 months ago

With the recent changes to generics in #443, it might not be too hard to support generic structs. As in:

struct Pair<A, B> {
  first: A,
  second: B,
}

or maybe

struct Vec<D> {
  x: D,
  y: D,
  z: D,
}
sharkdp commented 1 month ago

For the implementation of this function, I wanted to have something like

struct MixedUnitComponent<D: Dim> {
  unit: D,
  name: String,
}

in order to pass just one List<MixedUnitComponent<D>> argument around instead of two synchronized lists List<D> and List<String>.

sharkdp commented 1 month ago

This would also be useful for the implementation of the plotting functionality, which currently falls back to Scalars using value_of. And the unit names are transported in the string arguments :roll_eyes:

struct _PlotData {
  x_label: String, # something like "length [m]"
  y_label: String,
  xs: List<Scalar>,
  ys: List<Scalar>,
}

It would be much nicer to be able to write


struct _PlotData<X: Dim, Y: Dim> {
  x_label: String, # only "length"; unit can be determined from `xs`
  y_label: String,
  xs: List<X>,
  ys: List<Y>,
}
sharkdp commented 1 month ago

Early draft: https://github.com/sharkdp/numbat/pull/514

sharkdp commented 1 month ago

Another example where this is needed: #529