Skip to content

Add support to replace parameter with multiple different types (Like Generic parameter?) #9

@LokiMidgard

Description

@LokiMidgard

As in issue #8 sugestet, it may be nice if parameter and other Types could be replaced with different types.

From original issue

	[Mixin(typeof(Shared), nameof(Test), nameof(Test) )] // `class MixinAttribute( Type mixin, params String[] placeholderValues )`
	public partial class Test
	{
	}

	public class Shared
	{
		public static implicit operator Int32( [Placeholder(0)] Shared self )
		{
			return 1;
		}

		struct PlaceholderFoo {
			Shared GetShared() { throw new NotImplementedException(); } // or use an interface?
		}


                [TextPlaceholder(1, typeof(PlaceholderFoo))]
		[return: Placeholder(0)]
		public static Shared operator+( [Placeholder(0)]  Shared left, [Placeholder(0)] Shared right )
		{
			PlaceholderFoo foo = new PlaceholderFoo();
                        return foo.GetShared();
		}
	}

When the mixin is copied into the receiver class, all parameters (and return-types) tagged with PlaceholderAttribute(1) are
lexically replaced with "Test" (as it's specified in [Mixin(typeof(Shared), nameof(Test) )]) while type-names (or any text in general)
inside a member function can be specified using the hypothetical attribute [TextPlaceholder] while the method uses real
types (in this case struct PlaceholderFoo) which gets replaced with the second value from the receivers params
String[] placeholderValues, so the above is compiled to this:

    public partial class Test
    {
        public static implicit operator Int32( Test self )
        {
            return 1;
        }

	public static  Test operator+( Test left, Test right )
	{
		Test foo = new Test();
		return foo.GetShared();
	}
    }

This approach has the benefit of allowing the "template" source to still compile as valid C# (so we can avoid using T4) while >allowing more powerful functionality closer to C++ templates - without introducing the limitations of C#'s generics as a means >of parameterisation (as mentioned below).


ToDo

  • More specifications

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions