type-parameter

Base class constraint on generic class specifying the class itself

给你一囗甜甜゛ 提交于 2020-07-15 11:13:04
问题 Yesterday, I was explaining C#'s generic constraints to my friends. When demonstrating the where T : CLASSNAME constraint, I whipped up something like this: public class UnusableClass<T> where T : UnusableClass<T> { public static int method(T input){ return 0; } } And was really surprised to see it compile. After a bit of thinking, however, I figured it was perfectly legal from the point of view of the compiler - UnusableClass<T> is as much of a class as any other that can be used in this

C# - Specifying type parameters of super-class in sub-classes?

懵懂的女人 提交于 2020-04-18 06:10:12
问题 I am trying to do the following in C#. public class Parent<T> where T : Parent<???> { public T Prop { get; set; } } public class Child : Parent<Child> { } How can I do it? 回答1: This works fine: public class Parent<T> where T : Parent<T> { public T Prop { get; set; } } public class Child : Parent<Child> { } Do be careful with this as c# does not enforce a true Parent / Child relationship. For example, given the above code, it is also legal for me to then do this: public class Stranger : Parent

How to specify a type parameter in calling a function while let the compiler infer the other?

♀尐吖头ヾ 提交于 2020-01-25 03:42:05
问题 I have a class defined like this: implicit class TraversableLikeView[+A, +Repr, Raw](self: Raw)(implicit cast: Raw => TraversableLike[A,Repr]) { def filterByType[B, That](implicit bf: CanBuildFrom[Repr, B, That]): That = { val result = cast(self).flatMap{ case tt: B => Some(tt) case _ => None }(bf) result } } When calling it on TraversableLikeView("abc" :: "def" :: Nil) : I want type parameter B to be specified, and type parameter That to be automatically inferred from predefined implicits.

Kotlin - Generic Type Parameters Not Being Respected

不问归期 提交于 2020-01-22 14:33:22
问题 Consider the following example: import kotlin.reflect.KProperty1 infix fun <T, R> KProperty1<T, R>.test(value: R) = Unit data class Foo(val bar: Int) fun main() { Foo::bar test "Hello" } Given that test expects a value of type R , why in this context, where the property type is Int , does it allow me to pass a String ? 回答1: First, take a look at the declaration of the interface KProperty1 , which is: interface KProperty1<T, out R> : KProperty<R>, (T) -> R The important part here is the out

Scala missing parameter type in type class operator

孤街浪徒 提交于 2020-01-17 06:20:53
问题 I have the following code: class Pipe[ A ]( a: A ) { def |>[ B ]( f: A => B ) = f( a ) def !>[ B ]( f: A => B ) : Try[B] = Try(f( a )) def !>[ B, C ]( f: B => C )(implicit ev: A =:= Try[B]) : Try[C] = a.map(f) } (Implicit and apply not included) I am having problems with the "missing parameter type" error. The following code compiles correctly: val r1 = 5 |> (x => x + 1) However the following fails to compile: val r6 = 100 !> { x => x * 2 } Unless I write: val r6 = 100 !> { x : Int => x * 2 }

what is use cases of F# explicit type parameters?

╄→гoц情女王★ 提交于 2020-01-13 16:57:55
问题 As I know, explicit type parameters in value definitions is a one way to overcome "value restriction" problem. Is there another cases when I need to use them? Upd : I mean "explicitly generic constructs", where type parameter is enclosed in angle brackets, i.e. let f<'T> x = x 回答1: This would likely be rare, but when you want to prevent further generalization (§14.6.7): Explicit type parameter definitions on value and member definitions can affect the process of type inference and

what is use cases of F# explicit type parameters?

放肆的年华 提交于 2020-01-13 16:57:11
问题 As I know, explicit type parameters in value definitions is a one way to overcome "value restriction" problem. Is there another cases when I need to use them? Upd : I mean "explicitly generic constructs", where type parameter is enclosed in angle brackets, i.e. let f<'T> x = x 回答1: This would likely be rare, but when you want to prevent further generalization (§14.6.7): Explicit type parameter definitions on value and member definitions can affect the process of type inference and

Understanding “inferred type arguments do not conform to type parameter bounds” errors in Scala

最后都变了- 提交于 2020-01-10 19:29:49
问题 I fail to understand why I am getting an “inferred type arguments do not conform to type parameter bounds”. First, I defined a trait called CS which may be implemented by several classes (e.g., CS01 and CS02): trait CS[+T <: CS[T]] { this: T => def add: T def remove: T } class CS01 extends CS[CS01] { def add: CS01 = new CS01 def remove: CS01 = new CS01 } class CS02 extends CS[CS02] { def add: CS02 = new CS02 def remove: CS02 = new CS02 } The idea is to keep the implemented type when calling

“import and using may not appear after a type declaration” — the haxe using magic

♀尐吖头ヾ 提交于 2020-01-06 15:29:28
问题 I am trying to add an less-than-or-equal-to method (non-intrusively) to basic types such as Int, Float or existing/library types that I cannot change. (see my other question how to write a generic compare function in Haxe (haxe3)) I read that the "using" keyword is the way to do it. What I intend to do is this: class IntOrder { static public function le(x:Int,y:Int):Bool { return x <= y; } } class FloatOrder { static public function le(x:Float,y:Float):Bool { return x <= y; } } class

Implementing ListeningExecutorService in Java 5

删除回忆录丶 提交于 2020-01-04 13:47:52
问题 In Java 5, the ExecutorService interface declares the method: <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) throws InterruptedException; whereas Guava 11.0.2, written in Java 6 but supposedly compatible with Java 5, overrides it in ListeningExecutorService as: <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException; If I want to implement my own ListeningExecutorService , I would need to implement both of these methods, but I am also