Skip to content

Instantly share code, notes, and snippets.

@lucasmeijer
Last active December 2, 2017 19:26
Show Gist options
  • Save lucasmeijer/5d38d86b056cb0321b8f9b7bd1d5f9ee to your computer and use it in GitHub Desktop.
Save lucasmeijer/5d38d86b056cb0321b8f9b7bd1d5f9ee to your computer and use it in GitHub Desktop.

I'm writing an API method, with two arguments, both of which want to accept "one or more paths". A path in my case is an NPath reference, and I want to be able to call my method both like this:

var mypath1 = new NPath("mydir/myfile.txt");
var mypath2 = new Npath("file2");
myMethod(mypath, mypath2);

as well as with an IEnumerable

IEnumerable<NPath> paths1 = fromsomewhere();
IEnumerable<NPath> paths2 = fromsomewhereelse();
myMethod(paths1, paths2);

How would you write such a method?

I'd like to do this without overloads, since my method will more than 2 arguments eventually that should allow taking one or many, and the overload explosion just grows out of control. I tried writing a OneOrMorePaths struct that has an implicit operator from a NPath as well as from IEnumerable, but you're not allowed to write implicit operators that take interfaces as inputs.

One more constraint, I do not want NPath's to always be convertible to IEnumerable. only when the method explicitelly says it's ok to pass one or more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment