Interface - "Implements or Inherit", to make a new comparer class

Public forum to share code snippets, screen shorts, experiences, etc.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Interface - "Implements or Inherit", to make a new comparer class

Post by Phil Hepburn »

Hi Robert, Team, and guys,

I have come across a situation where two different 'approaches' seem to work. Please can you advise me on the best way to do things ?

I wish to make a comparer class for my LINQ query static methods, say to compare one Product with another. It would appear that I can use in X# syntax both 'Implements' and 'Inherit' and each works OK. Here is an image to help explain :-
RvdH_Interface_01.jpg
RvdH_Interface_01.jpg (123.88 KiB) Viewed 283 times
If I had to guess I would think that both approaches ends up with the same result - but then I don't write compilers ;-0)

Please can you advise me on the best way - if there is a best ! 

Oh! - maybe 'Implements' forces me to have both interface methods !?

HELP !!!

Happy New Year to all,
Phil.
Wales, UK.
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Interface - "Implements or Inherit", to make a new comparer class

Post by Frank Maraite »

Hi Phil,

I don't know is it intended or not. If I remember the C# syntax right there is no difference, it's the ':' sign.

BTW, both Equals and GetHashCode have to be implemented. This is forced in both ways.

It is important that GetHashCode returns equal values if Equals returns true for both objects. The opposite way this is not important. If Equals returns false GetHashCode may return the same value on both objects, but should return different values of course. Sorry I do not have a like to complete explanation. It has to do with the way hash tables are build.

Frank
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Interface - "Implements or Inherit", to make a new comparer class

Post by robert »

Phil,

Since X# builds on top of the C# Roslyn engine, and since C# uses the Colon ":" both for specifying parent classes and for interfaces there is no real difference between INHERIT and IMPLEMENTS anymore.
They both map to the same C# syntaxnode.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Interface - "Implements or Inherit", to make a new comparer class

Post by Phil Hepburn »

Thanks a lot Robert, for clearing that up with a straight forward explanation.

I had a funny feeling that this would be the case after doing the practical tests, and having a good deep think.

For your information I have been a bit silent, BUT, very productive over Christmas and New Year. I have some useful stuff to post in Pearls around the 'Seeding' of suitable test data to a 'Code First' SQL database, using LINQ and some other nice bits, including randomized population etc., including dates and times.

Just got my test 'Stock' app to use Entities to seed 4000 customer orders over a two year period, with more details to follow ..... Only takes a couple of seconds, and the code is nice and clear for any who wish to see it, and follow it through.

This is a big part to 'Code First' I feel, as good test data then allows us to design further code "features and facilities" which we can then test against the more meaningful data. Its very much a feed-back process. Better test data, better features, improved test data, etc., etc., etc..

Will post again in the morning when my mind is fresh ;-0)

I guess you have been away !?
"Happy New Year" to one and all.
Phil.
P.S. my soccer team Sunderland got a point today off Liverpool ;-0) 2 - 2 Draw, yippee !!!
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Interface - "Implements or Inherit", to make a new comparer class

Post by Frank Maraite »

Robert,
Robert van der Hulst wrote:Phil,

Since X# builds on top of the C# Roslyn engine, and since C# uses the Colon ":" both for specifying parent classes and for interfaces there is no real difference between INHERIT and IMPLEMENTS anymore.
They both map to the same C# syntaxnode.

Robert
then do you plan to change the syntax? Now we can write

IMPLEMENTS Ia, Ib
but only
INHERIT clsX

This is not possible
INHERIT clsX, Ia, Ib

Frank
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Interface - "Implements or Inherit", to make a new comparer class

Post by robert »

Frank,
Frank Maraite wrote:Robert,
then do you plan to change the syntax? Now we can write

IMPLEMENTS Ia, Ib
but only
INHERIT clsX

This is not possible
INHERIT clsX, Ia, Ib

Frank
We could do that. Do you think this is important ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Interface - "Implements or Inherit", to make a new comparer class

Post by Phil Hepburn »

Hey guys,

STOP - its okay the way the syntax is right now!

The reason .NET has Interfaces the way it does is because .NET does not support multiple inheritance. In one sense a crucial over-sight, but deliberate by MS.

We can presently do one 'inherit' and none, one, or more 'implements'. Fine.

The case I discussed is 'unique' in that in effect we can declare a new class with no inheritance, and implement from the interface - which is exactly the same as inheriting from the interface without any 'implements'. For this special case we can do both, and it does make sense.

It sounds confusing, but isn't if you have a mental 'visual' image of the general interface of an object - public members appearing over the surface of a sphere - the sphere is the object. We have a 'patch' of members from the basic class, and then other possible 'patches' of members from defined Interfaces.

I can draw you a nice picture if you like ;-0)

The ways things are then the "Three Pillars of OOPs" is quite concurrent it seems to me. So please don't break it.

Many of our colleagues misunderstand what an 'Interface' is, and possibly confuse the 'general interface' (GI) of an object, with the Public Members which make up a specific Interface.

To me, I feel that MS by using ' : ' for both situations, is not incorrect BUT misses a trick in not making the situation more clear to the coder / designer - as our Inherit and Implements does.

I hope this is clearer to you than it sounds to me typing - in fact the whole situation is quite simple, and Interfaces the way they are get around the fundamental inability to have multiple inheritance. Without which we are frankly 'buggered'.

What we have works ! Don't fix it (please).

Cheers,
Phil.
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Interface - "Implements or Inherit", to make a new comparer class

Post by Frank Maraite »

Robert,
Robert van der Hulst wrote:Frank,
Frank Maraite wrote:Robert,
then do you plan to change the syntax? Now we can write

IMPLEMENTS Ia, Ib
but only
INHERIT clsX

This is not possible
INHERIT clsX, Ia, Ib

Frank
We could do that. Do you think this is important ?

Robert
never thought about that before. But after reading Phil's answer I would say 'leave it as it is.'. Indeed it is more clear now. How much work is it to disallow inheriting an interface? This makes more sense I think.

We had the discussion about partial classes. I like it now better then before. One prg for each implemention of a specific interface and one for the rest. With inherit and interface keywords it cn be clearly organized.

Frank
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Interface - "Implements or Inherit", to make a new comparer class

Post by Phil Hepburn »

Hi guys,

I have found a good web short article on OOPs, 3 and 4 Pillars etc. It explains a lot AND probably forms the basis for leaving well alone in X#, or at least till all have had a good think and a full discussion on the matter.

I am not sure how to post a link successfully in the forum so here is my try(s)
http://themoderndeveloper.com/the-moder ... p-pillars/

http://themoderndeveloper.com/the-modern-developer/back-to-basics-three-or-four-oop-pillars/

Let me know if neither of these works please.

Cheers,
Phil.
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Interface - "Implements or Inherit", to make a new comparer class

Post by Frank Maraite »

Hi Phil,

both links work for me.

I like the site, becaue it's a summary of all I learned the last years.

But where is the relation to our syntax discussion?

Frank
Post Reply