4 ms·
All I can think coming away from this is: This still seems pretty useless, It'd be nice if java could allow for static interface methods. interface Progra
by yarg 4y ago
All I can think coming away from this is:
This still seems pretty useless, It'd be nice if java could allow for static interface methods.
interface Program {
static void main(String...args);
}
And then I digressed - heavily.
There have been several times when I've wanted static methods that apply to the specific class - although many of the situations could be shoehorned into a constructor, it's not the best.
You'd also want something like a 'This' meta-class keyword.
interface Foo extends Comparable<This> {
static This(A argA, B argB);
static This difference(This v0, This v1);
static int compare(This v0, This v1);
default int compareTo(This other){
return compare(v0, v1);
}
}
/*
I want to return an instance of my class' own type (defined in a subclass an so unknown),
so I pass every implementation itself as its own generic parameter.
*/
interface Why<W extends Why<W>> implements Comparable<W>{
W someManipulation();
}
class Foo implements Why<Foo> {
@Override
Foo someManipulation(){
....
}
}
To be honest, I think I reacted to how robust this does not feel.
It also blurs the line between static and instance syntax/functionality in a way that does not seem useful - and given that this is intended to reduce barriers to entry, is quite probably harmfully confusing.
- yarg 4y agoA better example of what I'm thinking (assuming these interfaces are in java.lang to allow for safe operator overloading): interface Summable { static This sum(This v0, This v1); } interface Subtractable { static This difference(This v0, This v1); } interface Divisible { static This quotient(This v0, This v1); } interface Mulitiplicable { static This product(This v0, This v1); } interface Number extends Summable, Subtractable, Divisible, Multiplicable; void <N extends Number> doThings(N v0, N v1){ N sum = v0 + v1; }
- evntdrvn 4y agoThe .NET team just added static interface members in C# 11, because they themselves had this need in order to create System.Numerics: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/static-virtual-interface-members https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tu... https://learn.microsoft.com/en-us/dotnet/api/system.numerics?view=net-7.0#interfaces https://learn.microsoft.com/en-us/dotnet/api/system.numerics...