[Mono-list] mcs won't allow implementation of interfaces with static methods

Jeroen Frijters jeroen@sumatra.nl
Thu, 15 Aug 2002 08:43:22 +0200


It's true that CSC is also broken, but that's no excuse for mcs to be
broken as well ;-)

C# doesn't allow you to declare static methods on interfaces, but I
don't see why mcs should complain when it consumes an interfaces that
happens to contain a static method. Also, the error messages doesn't
make any sense. BTW, the fix should be trivial and AFAICT it isn't
against the C# spec.

Regards,
Jeroen

> -----Original Message-----
> From: mono-list-admin@ximian.com 
> [mailto:mono-list-admin@ximian.com] On Behalf Of Serge
> Sent: Thursday, August 15, 2002 03:06
> To: mono-list@ximian.com
> Subject: Re: [Mono-list] mcs won't allow implementation of 
> interfaces with static methods
> 
> 
> > static methods are not allowed on interfaces
> 
> This is not allowed in C# but actually allowed in metadata.
> See Part II, 11 Semantics of Interfaces
> "Interfaces may have static fields and methods, but they 
> shall not have instance fields or methods."
> 
> However, I believe, CSC issues error message as well in this 
> case (when trying to implement interface with static methods).
> So perhaps MCS behaviour is correct as well.
> 
> However, it seems that CSC has another error in this respect 
> (I'm not 100% sure at the moment).
> 
> Consider the following ILASM code that has static member in interface.
> (This could be achieved with MC++ as well, I believe).
> 
> .assembly StaticIfc {}
> 
> .class interface public auto ansi StaticIfc {
>      .method public static int32 GetInt() cil managed {
>           ldc.i4 0xACDC
>           ret
>      }
> }
> 
> .class public auto ansi StaticImpl {
>      .method public static int32 GetInt() cil managed {
>           call int32 StaticIfc::GetInt()
>           ret
>      }
> }
> 
> 
> Now the following will compile correctly with CSC:
> 
>  public static void Main() {
>     Console.WriteLine("0x{0:X}", StaticImpl.GetInt());
>  }
> 
> While this will produce invalid code:
> 
>     Console.WriteLine("0x{0:X}", StaticIfc.GetInt());
> 
> It seems that MCS produces correct code in both cases.
> 
> 
> Sergey
> 
> 
> ----- Original Message ----- 
> From: "Martin Baulig" <martin@gnome.org>
> To: "Jeroen Frijters" <mono@jeroen.nu>
> Cc: <mono-list@ximian.com>
> Sent: Thursday, August 15, 2002 2:36 AM
> Subject: Re: [Mono-list] mcs won't allow implementation of 
> interfaces with static methods
> 
> 
> > "Jeroen Frijters" <mono@jeroen.nu> writes:
> > 
> > > When I have a C# class that tries to implement an 
> interface (created
> > > with ILASM) that contains a static method, mcs gives an 
> error saying
> > > that I should implement the static method (which 
> obviously doesn't make
> > > sense).
> > 
> > Hi,
> > 
> > I don't understand what you're trying to do: static methods are not
> > allowed on interfaces, that'd be an error CS0106.  MCS currently
> > doesn't report that error but abort with a parsing error 
> instead since
> > its parser won't recognize `static' as a keyword if it's used on an
> > interface method.
> > 
> > ====
> > using System;
> > 
> > interface A
> > {
> > static void Test ();
> > }
> > 
> > class X : A
> > {
> > public static void Test ()
> > {
> > }
> > 
> > static void Main ()
> > {
> > }
> > }
> > ====
> > 
> > -- 
> > Martin Baulig
> > martin@gnome.org
> > 
> 
> 
> 
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>