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 System.Collections.Immutable types #250

Closed
latonz opened this issue Jan 23, 2023 · 10 comments
Closed

Support System.Collections.Immutable types #250

latonz opened this issue Jan 23, 2023 · 10 comments
Assignees
Labels
enhancement New feature or request

Comments

@latonz
Copy link
Contributor

latonz commented Jan 23, 2023

Support collection types in System.Collections.Immutable as mapping target types.
Eg. Mapperly should be able to implement a mapping from an IEnumerable<long> to an ImmutableList<int>.

@latonz latonz added the enhancement New feature or request label Jan 23, 2023
@jamesfoster
Copy link

While this feature is not implemented is there a generic way to work around this?

Something like:

private static ImmutableList<TDest> ToImmutableList<TDest, TSource>(
    IEnumberable<TSource> source,
    Func<TSource, TDest> map
)
{
   return source.Select(map).ToImmutableList();
}

We can obviously add a workaround for each type but it doesn't seem to be possible to implement it generically.

@latonz
Copy link
Contributor Author

latonz commented Mar 28, 2023

Currently there is no support for generic user implemented mapping methods... I can't think of a feasible workaround 😢

@jamesfoster
Copy link

Thanks for the response. Without knowing too much about how source generators work, would it not be possible to support the suggested code? This could be useful for any generic type with a single type argument. if you know how to map from TSource to TDest you can supply a value for map. You can then use this method to map from any A<TSource> to a B<TDest>.

@TimothyMakkison
Copy link
Collaborator

TimothyMakkison commented Mar 28, 2023

Currently there is no support for generic user implemented mapping methods... I can't think of a feasible workaround 😢

I'll give it a go. Looks like EnumerableMappingBuilder.ResolveCollectMethodName can be used to add ToImmutableList etc. Not sure which immutable collections should be added aside from ImmutableList, ImmutableArray and ImmutableDictionary.

@latonz
Copy link
Contributor Author

latonz commented Mar 28, 2023

@TimothyMakkison nice 😊

The immutable collections are available in the immutable namespace. IMO Mapperly should probably support all generic implementations:

  • ImmutableList<T>
  • ImmutableArray<T>
  • ImmutableHashSet<T>
  • ImmutableQueue<T>
  • ImmutableSortedSet<T>
  • ImmutableDictionary <TKey, TValue>
  • ImmutableSortedDictionary<TKey,TValue>

Some may need more effort than others, therefore it could make sense to split this up into multiple PR's.

@latonz
Copy link
Contributor Author

latonz commented Mar 28, 2023

@jamesfoster this could probably be a new feature of Mapperly. However, I think it adds a lot of complexity and I'm not sure if it justifies the added complexity.

@jamesfoster
Copy link

@jamesfoster this could probably be a new feature of Mapperly. However, I think it adds a lot of complexity and I'm not sure if it justifies the added complexity.

No worries. My use case was immutable collections anyway, and it looks like you're working on it. I can work around it for the time being by adding the non-generic mapper for each type as needed.

Thanks.

@latonz
Copy link
Contributor Author

latonz commented Apr 12, 2023

#305 implements support for the following immutable collections:

  • ImmutableList<T>
  • ImmutableArray<T>
  • ImmutableHashSet<T>
  • ImmutableQueue<T>
  • ImmutableSortedSet<T>

Still missing:

  • ImmutableDictionary<TKey, TValue>
  • ImmutableSortedDictionary<TKey,TValue>

Thanks to @TimothyMakkison for the contribution 🎉

@latonz
Copy link
Contributor Author

latonz commented Apr 18, 2023

Thanks to another great contribution from @TimothyMakkison ImmutableDictionary<TKey, TValue> and ImmutableSortedDictionary<TKey,TValue> are now supported too.

@latonz
Copy link
Contributor Author

latonz commented Apr 20, 2023

This issue has been released in version 2.8.0-next.2 which is available on nuget.

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

No branches or pull requests

3 participants