Skip to content

Instantly share code, notes, and snippets.

@ilya-g
Last active November 12, 2015 18:43
Show Gist options
  • Save ilya-g/2571fff3891c22d97158 to your computer and use it in GitHub Desktop.
Save ilya-g/2571fff3891c22d97158 to your computer and use it in GitHub Desktop.
Resolving between element and list
public static class TestResolve
{
public static List<T> Append<T>(this IEnumerable<T> list, T element) { throw new NotImplementedException(); }
public static List<T> Append<T>(this IEnumerable<T> list, IEnumerable<T> elements) { throw new NotImplementedException(); }
public static void Usage()
{
var list = new List<List<String>>();
var element = new List<String>();
var concatE = list.Append(element); // List<List<String>>
var concatL = list.Append(list); // List<List<String>>
// var concatI = list.Append(1); // error on 1
var concatI = list.Append(1 as Object); // List<object>
var concatS = list.Append("chars".Cast<Object>()); // List<IEnumerable<object>>
}
}
fun <T> Iterable<T>.append(element: T): List<T> = TODO()
fun <T> Iterable<T>.append(list: Iterable<T>): List<T> = TODO()
val list = listOf(listOf("s"))
val element = listOf("a")
val concatE = list.append(element) // List<Any> resolved to append(list)
val concatL = list.append(list) // List<List<String>> resolved to append(list)
val concatI = list.append(1) // List<Any> resolved to append(element)
val concatA = list.append(element as List<Any>) // List<Any> resolved to append(list)
class ListLike[+A]
def add[A](t: ListLike[A], element: A): ListLike[A] = ???
def add[A](t: ListLike[A], list: ListLike[A]): ListLike[A] = ???
val listLike = new ListLike[ListLike[String]]()
val element = new ListLike[String]()
val concatE = add(listLike, element) // : ListLike[ListLike[String]]
val concatL = add(listLike, listLike) // : ListLike[ListLike[String]]
val concatI = add(listLike, 1) // : ListLike[Any] resolved to add(element)
val concatA = add(listLike, element: ListLike[Any]) // : ListLike[Any] resolved to add(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment