.NET Fiddle

CSharp · Console

/Program.cs

#nullable enable
using System;

public class Program
{
	public static void Main()
	{
		// works
		Func<bool, bool?> processor = x => !x;
		bool a = Process(false, processor);
		
		// works
		bool b = Process<bool, bool>(false, x => !x);
		
		// doesn't compile
		bool c = Process(false, x => !x);
		
		// these must also keep working
		Program d = Process(new Program(), x => x);
		bool e = Process<bool, bool>(false, x => x ? x : null);
		bool f = Process(false, x => x ? x : null);
	}

	public static TResult Process<TInput, TResult>(TInput input, Func<TInput, TResult?> processor)
		where TResult : struct
	{
		return processor(input) ?? throw new Exception();
	}
	
	public static TResult Process<TInput, TResult>(TInput input, Func<TInput, TResult?> processor)
		where TResult : class
	{
		return processor(input) ?? throw new Exception();
	}
	
	//public static TResult Process<TInput, TResult>(TInput input, Func<TInput, TResult?> processor)
	//	where TResult : notnull
	//{
	//	return processor(input) ?? throw new Exception();
	//}
	
	//public static TResult Process<TInput, TResult>(TInput input, Func<TInput, TResult> processor)
	//	where TResult : struct
	//{
	//	return processor(input);
	//}
}
;