C
C#10mo ago
DeltaDizzy

Using a concrete derived type in place of an interface in a constructor signature

I have a base class whose constructor takes in an instance of a generic interface: public Odometry(IKinematics<TSpeeds, TPositions> kinematics, ... ) in a derived class's constructor, in its place I have a type that fully implements that interface: DifferentialDriveKinematics : IKinematics<DifferentialDriveWheelSpeeds, DifferentialDriveWheelPositions> but the language complains that there is no corresponding argument: There is no argument given that corresponds to the required parameter 'kinematics' of 'Odometry<DifferentialDriveWheelSpeeds, DifferentialDriveWheelPositions>.Odometry(IKinematics<DifferentialDriveWheelSpeeds, DifferentialDriveWheelPositions>, Rotation2d, DifferentialDriveWheelPositions, Pose2d)' is that just not something that can be done (i.e. the interface argument type has to be there all the way down the inheritance chain without any further specialization until something is actually passed in)? The API design is such that I would prefer not to leak the interface to the end user and want the particular concrete type to be part of the constructor signature.
2 Replies
Angius
Angius10mo ago
Signatures are usually pretty exact You can try using a base constructor instead
public Whatever(DifferentialDriverKinematics kinematics) : base(kinematics) {}
public Whatever(DifferentialDriverKinematics kinematics) : base(kinematics) {}
DeltaDizzy
DeltaDizzyOP10mo ago
it had actually generated that to begin with and id deleted it because i didnt think it was necessary, but that does it, thanks!
Want results from more Discord servers?
Add your server