|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
To extend Function or not to extend Function
Consider the following case:
You want to make an interface which forces the functionality of being interpolable, that is, being able to be interpolated. You'd probably start off with the following code Code:
public interface Interpolable {
/**
* Interpolates this value and @param a
*/
public Interpolable interpolate(Interpolable a);
}
If the interpolate method accepts a, of type Interpolable, and returns some Interpolable, then any implementing class could accept and return any other implementing class. Desired functionality would limit both of these to only the type of the class which is doing the implementing. My solution: Code:
public interface Interpolable<T extends Interpolable<T>> {
/**
* Interpolates this value and @param a
*/
public T interpolate(T a);
}
Say you also want your Interpolable interface extend UnaryOperator, because it takes in an Object and returns an Object, both of which are of type T. However, the method required by UnaryOperator is T apply(T a). A way to get around that would be to make Interpolable an abstract class and make the method interpolate call apply. Are there more elegant ways to do this? Code:
public abstract class Interpolable<T extends Interpolable<T>> implements UnaryOperator<T> {
/**
* Interpolates this value and @param a
*/
public T interpolate(T a) {
return apply(a);
}
}
|
|
#2
|
||||
|
||||
|
Re: To extend Function or not to extend Function
Why do you need to restrict "imposter" classes? I think your second code block has the best solution.
|
|
#3
|
|||||
|
|||||
|
Re: To extend Function or not to extend Function
Concur. The purpose of the interface is to allow a totally distinct implementation of the interface contract. While I agree that there is no clear way for an interface to enforce a contract, it is only trivially more difficult to "break the contract" for a class which extends a base class. (How many times have programmers overridden object.equals to mean something quite different than "is-another-handle-for-the-same-object"?)
|
|
#4
|
||||
|
||||
|
Re: To extend Function or not to extend Function
+1 to what has already been said. It's up to each code implementor to ensure that the proper functionality is in place, and up to reach consumer of code that others have written to ensure that code does what they want.
To offer concrete proof that this isn't possible in Java: Suppose there were a way to enforce what you're asking for Code:
public interface A<T> /* magic that requires subclass of A to use itself as the type parameter */ {
public T interpolate(T a);
}
Code:
class B extends A<B> {
public B interpolate(B a){
// implementation
}
}
class C extends B {}
Code:
class C extends A<B> |
|
#5
|
|||
|
|||
|
Re: To extend Function or not to extend Function
Makes sense. Thanks for the reply
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|