Re: operator overloading



Owen Jacobson wrote:
On May 8, 8:10 pm, Tom Anderson <t...@xxxxxxxxxxxxxxx> wrote:
Also, can i declare:

public class Vector implements Multipliable<double, Vector>,
Multipliable<Vector, double>, Multipliable<Matrix, Vector>

?

No, as double is not a reference type; no, as you can't implement an
interface more than once and all those generic specializations are
really the same interface. The former can be worked around with the
language as-is; the latter cannot. Allowing that would involve
changing the way method overloads are resolved, or adding rules to
forbid implementing both Multipliable<Double, Vector> and
Multipliable<Vector, Vector> - as it stands, you can't provide both
public Double multiply (Vector) and public Vector multiply (Vector) in
the same class, because the only distinction is the return type.

Multipliable<Double, Vector> would essentially provide the same service as Multipliable<double, Vector> thanks to auto-(un)boxing.

Also (as the one who posted with this proposal), the idea of LHS operator overloading cannot be satisfactorily be solved in this manner; I've been racking my brains for a solution and have found known (more discussion in my earlier thread 'Java 7 Features').

This would be Tom's proposed declaration (I believe he munged the syntax), then:

public class Vector implements Multipliable<Double, Vector> {
}

public class Matrix implements Multipliable<Double, Matrix>,
Multipliable<Matrix, Matrix>, Multipliable<Vector, Vector> {
}

Unfortunately, to do so would still require modification such that a class can implement the same generic interface multiple times with different generic arguments--i.e., reification of generics.

--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.



Relevant Pages

  • Re: Casting Generic Classes - Possible Solution
    ... It leaves me with the open option to use generics or not when I ... This works as saying "accept a store of anything that are Record ... public class Record { ... So I tried adding an interface into the structure: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to use generics?
    ... getting rid of the ACollection and BCollection and just having Collectionwould be a good start towards using generics to the full extent. ... public void AddCollection() ... public bool ContainsKey ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Feature request for C# Generic inheritence
    ... The trouble is that .NET generics, unlike C++ templates, are completely ... compile time. ... public class GenericDecorator: T ...
    (microsoft.public.dotnet.languages.csharp)
  • Workaround to overcome the generics limitation causes by Erasure
    ... I thought the introduce of generics in Java would solve me ... Observer/Subject pattern. ... public void attach(Observer<S, A> observer) { ... public class MainFrame extends javax.swing.JFrame implements ...
    (comp.lang.java.programmer)
  • Re: how to find the type of an object represented by an interface
    ... Simplifying, generics are strictly linked to compilation, not runtime. ... template parameter T must implement particular interface i.e. IDisposable: ... Readonly property MaximumRange() as Integer ...
    (microsoft.public.dotnet.framework.aspnet)