generics

Typescript TSX and generic parameters

非 Y 不嫁゛ 提交于 2020-11-30 20:48:55
问题 Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones: const f = <T1>(arg1: T1) => <T2>(arg2: T2) => { return { arg1, arg2 }; } I wonder is there a way to make it work inside a *.tsx file? 回答1: You could use function expressions instead: const f = function<T1>(arg1: T1) { return function<T2>(arg2: T2) { return { arg1, arg2 }; }; }; Or alternatively, I've discovered this works: const f = <T1, T2>(arg1:

Typescript TSX and generic parameters

ぃ、小莉子 提交于 2020-11-30 20:42:49
问题 Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones: const f = <T1>(arg1: T1) => <T2>(arg2: T2) => { return { arg1, arg2 }; } I wonder is there a way to make it work inside a *.tsx file? 回答1: You could use function expressions instead: const f = function<T1>(arg1: T1) { return function<T2>(arg2: T2) { return { arg1, arg2 }; }; }; Or alternatively, I've discovered this works: const f = <T1, T2>(arg1:

Typescript TSX and generic parameters

泪湿孤枕 提交于 2020-11-30 20:39:32
问题 Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones: const f = <T1>(arg1: T1) => <T2>(arg2: T2) => { return { arg1, arg2 }; } I wonder is there a way to make it work inside a *.tsx file? 回答1: You could use function expressions instead: const f = function<T1>(arg1: T1) { return function<T2>(arg2: T2) { return { arg1, arg2 }; }; }; Or alternatively, I've discovered this works: const f = <T1, T2>(arg1:

Typescript TSX and generic parameters

蹲街弑〆低调 提交于 2020-11-30 20:38:25
问题 Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones: const f = <T1>(arg1: T1) => <T2>(arg2: T2) => { return { arg1, arg2 }; } I wonder is there a way to make it work inside a *.tsx file? 回答1: You could use function expressions instead: const f = function<T1>(arg1: T1) { return function<T2>(arg2: T2) { return { arg1, arg2 }; }; }; Or alternatively, I've discovered this works: const f = <T1, T2>(arg1:

Typescript TSX and generic parameters

拟墨画扇 提交于 2020-11-30 20:35:18
问题 Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones: const f = <T1>(arg1: T1) => <T2>(arg2: T2) => { return { arg1, arg2 }; } I wonder is there a way to make it work inside a *.tsx file? 回答1: You could use function expressions instead: const f = function<T1>(arg1: T1) { return function<T2>(arg2: T2) { return { arg1, arg2 }; }; }; Or alternatively, I've discovered this works: const f = <T1, T2>(arg1:

Why is there an extra <E> in this generic method?

烈酒焚心 提交于 2020-11-30 06:38:37
问题 I learned java generics some time ago, but now I'm learning collections and found some code that I don't understand. Here is the code: static <E> List<E> nCopies(int n, E value) It is from class java.util.Collections . My question is why there is: <E> List<E> and not only List<E> Obviously I am missing something, can someone clarify this for me? 回答1: In <E> List<E> , the first <E> denotes that E is a type parameter . If you hadn't specified it, then Java would think the E in E value referred

My implementation of Swap<T> does not work?

三世轮回 提交于 2020-11-30 02:03:53
问题 So I write below quick code that should be working for refernce and struct types, but first one does not do anything useful at all and second is useless for List<int> . Can anyone point out bugs and design issues in below code? public static void Swap<T>(T o1, T o2) where T : class, IComparable<T> { if (o1.CompareTo(o2) != 0) { var temp = o1; o1 = o2; o2 = temp; } } public static void Swap<T>(ref T o1, ref T o2) where T : struct, IComparable<T> { if (o1.CompareTo(o2) != 0) { var temp = o1; o1

My implementation of Swap<T> does not work?

荒凉一梦 提交于 2020-11-30 02:00:05
问题 So I write below quick code that should be working for refernce and struct types, but first one does not do anything useful at all and second is useless for List<int> . Can anyone point out bugs and design issues in below code? public static void Swap<T>(T o1, T o2) where T : class, IComparable<T> { if (o1.CompareTo(o2) != 0) { var temp = o1; o1 = o2; o2 = temp; } } public static void Swap<T>(ref T o1, ref T o2) where T : struct, IComparable<T> { if (o1.CompareTo(o2) != 0) { var temp = o1; o1

What's different between “def apply[T](c:T)” and “type T;def apply(c:T)”

℡╲_俬逩灬. 提交于 2020-11-29 21:17:37
问题 I have this program: object B{ def apply[T](c:T)={} } object C{ type T def apply(c:T)={} } object A extends App{ val d=B{println(1);2} val e=C{println(1);2} } the line val e = C{println(1);2} told me error:Type mismatch,expected C.T,actual:2 so why can't I write type T def apply(c:T) it seems the same as apply[T](c:T) and what's the type of T when I write val d=B{println(1);2} I can write many lines here! because T means generic,so it can be Int,String,user defined class Apple,Orange... and

What's different between “def apply[T](c:T)” and “type T;def apply(c:T)”

心不动则不痛 提交于 2020-11-29 21:17:00
问题 I have this program: object B{ def apply[T](c:T)={} } object C{ type T def apply(c:T)={} } object A extends App{ val d=B{println(1);2} val e=C{println(1);2} } the line val e = C{println(1);2} told me error:Type mismatch,expected C.T,actual:2 so why can't I write type T def apply(c:T) it seems the same as apply[T](c:T) and what's the type of T when I write val d=B{println(1);2} I can write many lines here! because T means generic,so it can be Int,String,user defined class Apple,Orange... and