On Wednesday, 17 April 2024 at 01:36:59 UTC, Liam McGillivray
wrote:
To better understand what I mean, take the following example,
where I have a function, and two structs.
```
struct typeA {
// Some member variables here
}
struct typeB {
// Some similar member variables here, but in a different
format
}
float someFunction(typeB input) {
// Does some stuff
// Returns result
}
```
If I want to be able to call `someFunction` (or any function
with `TypeB` as a parameter) using `TypeA` in place of `TypeB`,
and I'm willing to modify the definition of `TypeA`, I know
that I can add an `opCast` and `alias this = opCast!TypeB` to
`TypeA`.
But what if `typeA` is in an external library? Is there any way
I can get `someFunction` (and any function with a `typeB`
parameter) to accept `typeA`, only modifying the definition of
`TypeB` (and possibly adding a single global line in it's
module)?
This is called [row polymorphism][1], and it does not exist in D.
You could approximate it by making `someFunction` a template, and
accepting any type `T` that has the necessary members instead of
only accepting `typeB`. But this is only possible if you are free
to modify the definition of `someFunction`.
[1]: https://en.wikipedia.org/wiki/Row_polymorphism