type-constraints

How to call a generic method with type constraints when the parameter doesn't have these constraints?

喜你入骨 提交于 2019-12-24 01:18:31
问题 class A { } interface I { } void GenericStuff<T>(T x) { } void SpecificStuff<T>(T x) where T : A, I { } void Start<T>(T x) { if (x is A && x is I) SpecificStuff(x); // <---- Wrong type else GenericStuff(x); } I've got the situation illustrated above. In method Start() I get a single parameter x and depending on it's type I want to call either the GenericStuff() or the SpecificStuff() method. Naturally, the type constraints prevent me from doing so, and since there are two of them, I cannot

Partially applied equality constraint

时光总嘲笑我的痴心妄想 提交于 2019-12-23 17:10:19
问题 Consider the following: class (a ~ b) => Equal a b instance (a ~ b) => Equal a b Lets say I later have a data type: data D (c :: * -> Constraint) where D :: Proxy c -> D c Then something like this is valid: D (Proxy :: (Proxy (Equal Int))) My question is, is there any way to write Equal other than the repetitive class/instance notation I've used? It seems that when I define Equal as a type family Equal Int doesn't work because it's an incomplete application. The class/instance hack looks bad

How to constraint a generic to be of type enum?

廉价感情. 提交于 2019-12-23 08:54:25
问题 Consider the following code: class Base<T> { //Base members } I want the generic T to be an enum (using constraints if possible). How can I do this in C#? EDIT: Using code contracts -introduced by Akash Kava- also seems like a nice way. I managed to get it to produce a run time error which is useless. Here's the code I tried. It should be possible to generate a compile time warning but I can't get it to work. 回答1: This is supported at the IL level but not in C#. You may take a look at

C#'s can't make `notnull` type nullable

亡梦爱人 提交于 2019-12-23 07:55:17
问题 I'm trying to create a type similar to Rust's Result or Haskell's Either and I've got this far: public struct Result<TResult, TError> where TResult : notnull where TError : notnull { private readonly OneOf<TResult, TError> Value; public Result(TResult result) => Value = result; public Result(TError error) => Value = error; public static implicit operator Result<TResult, TError>(TResult result) => new Result<TResult, TError>(result); public static implicit operator Result<TResult, TError>

The case of the disappearing constraint: Oddities of a higher-rank type

十年热恋 提交于 2019-12-23 07:25:38
问题 All the experiments described below were done with GHC 8.0.1. This question is a follow-up to RankNTypes with type aliases confusion. The issue there boiled down to the types of functions like this one... {-# LANGUAGE RankNTypes #-} sleight1 :: a -> (Num a => [a]) -> a sleight1 x (y:_) = x + y ... which are rejected by the type checker... ThinAir.hs:4:13: error: * No instance for (Num a) arising from a pattern Possible fix: add (Num a) to the context of the type signature for: sleight1 :: a -

haskell — rank n constraints? (or, monad transformers and Data.Suitable)

北慕城南 提交于 2019-12-22 05:07:54
问题 I'm trying to write something that appears to be analagous to "rank 2 types", but for constraints instead. (Or, maybe it's not correct to assume changing -> in the definition of "rank 2 types" to => is meaningful; please edit the question if you think up better terminology). setup First, the Suitable typeclass (from Data.Suitable, the base of rmonad) can be used to denote types of values which can be used. In this question, I'll use Suitable m a to denote that value a can be used as a value

Is it possible to constrain a C# generic method type parameter as “assignable from” the containing class' type parameter?

醉酒当歌 提交于 2019-12-22 04:09:51
问题 I suspect the answer is no, but I want to know if it is possible to do something like this: public class MyGenericClass<TSomeClass> { public void MyGenericMethod<TSomeInterface>() // This doesn't compile. where TSomeClass : TSomeInterface { //... } } What I mean to indicate in the above (non-working) example is to constrain TSomeInterface such that it can be any base class, implemented interface, or (if you really want to get fancy) implicit conversion of MyGenericClass . NOTE: I suspect that

Why does Haskell stop short of inferring the datatype's typeclasses in the function signatures?

我的未来我决定 提交于 2019-12-21 07:08:21
问题 Firstly, this question isn't 100% specific to Haskell, feel free to comment on the general design of typeclasses, interfaces and types. I'm reading LYAH - creating types and typeclasses The following is the passage that I'm looking for more information on: Data (Ord k) => Map k v = ... However, it's a very strong convention in Haskell to never add typeclass constraints in data declarations. Why? Well, because we don't benefit a lot, but we end up writing more class constraints, even when we

How does the <:< operator work in Scala?

前提是你 提交于 2019-12-21 02:36:06
问题 In Scala there's a class <:< that witnesses a type constraint. From Predef.scala : sealed abstract class <:<[-From, +To] extends (From => To) with Serializable private[this] final val singleton_<:< = new <:<[Any,Any] { def apply(x: Any): Any = x } implicit def $conforms[A]: A <:< A = singleton_<:<.asInstanceOf[A <:< A] An example of how it's used is in the toMap method of TraversableOnce : def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = What I don't understand is how this

Generic constructor with new type constraint

耗尽温柔 提交于 2019-12-20 04:45:06
问题 I have 2 types of objects, Database models and normal system models. I want to be able to convery the model into Database model and vice versa. I have the following method I wrote: public static E FromModel<T, E>(T other) where T : sysModel where E : dbModel { return new E(other); } basically both sysModel and dbModel are abstract. dbModel have lots of inherting classes which all have copy constructors. Im receiving : Cannot create an instance of type parameter 'E' becauase it does not have