Skip to content

Conversation

@jpobst
Copy link
Contributor

@jpobst jpobst commented Aug 29, 2019

Using the new DIM support we can now also support static interface methods/properties.

Example:

public interface StaticMethodsInterface
{
    static int foo () { return 10; }
    static int getValue () { return 3; }
    static void setValue (int value) { }
}

Can now be invoked:

[Test]
public void TestStaticMethods ()
{
    Assert.AreEqual (10, IStaticMethodsInterface.Foo ());
			
    Assert.AreEqual (3, IStaticMethodsInterface.Value);
    Assert.DoesNotThrow (() => IStaticMethodsInterface.Value = 5);
}


[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.")]
public abstract class MyInterfaceConsts : MyInterface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we emitting an empty MyInterfaceConsts type? If the interface had some constants, that would be one thing, but it doesn't...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MyInterfaceConsts type is always generated if we generate the MyInterface class. It does not attempt to detect why we are writing a MyInterface class.

Logic for determining if we are writing a MyInterface class:

interface.Fields.Any () || interface.Methods.Where (m => m.IsStatic)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we alter that logic? Particularly considering that the generated type is [Obsolete] (lol?), and there's no reason for the type to exist in a C#8 default-interface-members world order?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically a break if an existing library enables DIM, so we discussed making these [Obsolete(error:true)] so maybe eventually we could remove them. (For a separate PR.)

@jpobst jpobst force-pushed the static-interface-methods branch from ddbdfae to d6b7833 Compare August 29, 2019 19:52
@jpobst jpobst force-pushed the static-interface-methods branch from d6b7833 to fe4deb0 Compare August 29, 2019 19:54
@jonpryor jonpryor merged commit 855b7e9 into master Aug 30, 2019
@jonpryor jonpryor deleted the static-interface-methods branch August 30, 2019 15:12
jonpryor pushed a commit that referenced this pull request Sep 6, 2019
Using the new Default Interface Members support (29f9707), and
`generator --lang-features=default-interface-methods`, we can now
also support static interface methods/properties.

This Java interface:

	// Java
	public interface StaticMethodsInterface {
	    static int foo () { return 10; }
	    static int getValue () { return 3; }
	    static void setValue (int value) { }
	}

can now be bound as:

	// C# Binding
	public partial interface IStaticMethodsInterface {
	    static new readonly JniPeerMembers _members = new JniPeerMembers (
	            "java/code/StaticMethodsInterface",
	            typeof (IStaticMethodsInterface),
	            isInterface: true);

	    public static unsafe int Foo ()
	    {
	        return _members.StaticMethods.InvokeInt32Method ("foo.()I", null);
	    }

	    public static unsafe int Value {
	        get {
	            return _members.StaticMethods.InvokeInt32Method ("getValue.()I", null);
	        }
	        set {
	            JniArgumentValue* __args = stackalloc JniArgumentValue [1];
	            __args [0] = new JniArgumentValue (value);
	            _members.StaticMethods.InvokeVoidMethod ("setValue.(I)V", __args);
	        }
	    }
	}

Which can be invoked as:

	[Test]
	public void TestStaticMethods ()
	{
	    Assert.AreEqual (10, IStaticMethodsInterface.Foo ());
				
	    Assert.AreEqual (3, IStaticMethodsInterface.Value);
	    Assert.DoesNotThrow (() => IStaticMethodsInterface.Value = 5);
	}
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants