Making one struct work in place of another for function calls.
Liam McGillivray
yoshi.pit.link.mario at gmail.com
Wed Apr 17 01:36:59 UTC 2024
I have two structs that serve roughly the same purpose, and I
would like one to be accepted when the other is declared as a
function parameter.
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)?
More information about the Digitalmars-d-learn
mailing list