Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Func<> resolution #166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Support Func<> resolution #166

wants to merge 4 commits into from

Conversation

pakrym
Copy link
Owner

@pakrym pakrym commented Dec 30, 2023

No description provided.

@pakrym pakrym changed the title Support Func<> resoultion Support Func<> resolution Dec 30, 2023
@MitchRazga
Copy link
Contributor

@pakrym Is this complete but just not published?

Played around with this branch earlier and Func<> resolution seemed to work as expected. Tests were passing too.

@MitchRazga
Copy link
Contributor

Did run into one issue when multiple constructors use implicit funcs it will always choose the first one defined even if the service is not registered/optional. eg.

using Jab;
using System.Diagnostics;

internal class Program
{
	private static void Main(string[] args)
	{
		using Services services = new();
		IProvider<bool> provider = services.GetService<IProvider<bool>>();
	}
}

[ServiceProvider]
[Singleton(typeof(IProvider<>), typeof(Provider<>))]
//[Singleton(typeof(IService1<>), typeof(Service1<>))]
[Singleton(typeof(IService2<>), typeof(Service2<>))]
internal sealed partial class Services;

public interface IService1<T>;

public class Service1<T> : IService1<T>;

public interface IService2<T>;

public class Service2<T> : IService2<T>;

public interface IProvider<T>;

public class Provider<T> : IProvider<T>
{
	//public Provider(IService1<T> factory) => Debugger.Break();
	public Provider(Func<IService1<T>> factory) => Debugger.Break(); // Always hit even when not the best match
	//public Provider(Func<IService1<T>>? factory = null) => Debugger.Break(); // Same as above. Always hit.

	//public Provider(IService2<T> factory) => Debugger.Break();
	public Provider(Func<IService2<T>> factory) => Debugger.Break(); // Never hit unless moved above other constructors
}

@pakrym
Copy link
Owner Author

pakrym commented Dec 28, 2024

@MitchRazga this should work reasonably enough but I didn't have time for thorough testing.

I wonder is the constructor selection issue you see if related to funcs or a general bug.

@pakrym
Copy link
Owner Author

pakrym commented Feb 6, 2025

@eXpl0it3r / @MitchRazga / @sensslen any interest in push this over the finish line? :)

@MitchRazga
Copy link
Contributor

Other than the multiple constructor issue mentioned above, it seemed pretty good as is.

I tried step-through debugging the source generator but still didn't have enough understanding to be able to fix it.

@MitchRazga
Copy link
Contributor

Looks like in SelectConstructor that CanSatisfy always returns false for Func<> and will therefore fallback to the first/shortest constructor.

// Pick a shortest candidate just in case we don't find
// any applicable ctor and need to produce diagnostics
if (candidate == null ||
candidate.Parameters.Length > constructor.Parameters.Length)
{
candidate = constructor;
}
// Return a candidate so we can produce diagnostics for required services in a simple case
return selectedCtor ?? candidate;

Might need to something here to first extract the service type from the Func<>

if (genericType != null &&
	SymbolEqualityComparer.Default.Equals(genericType.ConstructedFrom, _knownTypes.FuncType) &&
		genericType.TypeArguments[0] is INamedTypeSymbol funcServiceType)
{
	serviceType = funcServiceType;
}

Did a quick test offline and worked as expected. Tests were still passing too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants