FAQ Database Discussion Community


Generic constraint of X AND Y

c#,.net,generics,type-constraints
Can generic constraints be used to enforce a constraint on type derivatives of an abstract class, but only those that implement an interface? Example: abstract class Dependency { public abstract void IMustDoThis(); } interface IOptionalDependency { void IMightDoThis(); } sealed class AlmightyDependency : Dependency, IOptionalDependency { // I have sealed...

Why does this violate the type constraint?

asp.net,asp.net-identity,type-constraints,asp.net-identity-3
I'm trying to customise ASP.NET Identity 3 so that it uses integer keys: public class ApplicationUserLogin : IdentityUserLogin<int> { } public class ApplicationUserRole : IdentityUserRole<int> { } public class ApplicationUserClaim : IdentityUserClaim<int> { } public sealed class ApplicationRole : IdentityRole<int> { public ApplicationRole() { } public ApplicationRole(string name) { Name...

Bundling constraints with fundeps

haskell,ghc,functional-dependencies,type-constraints
I've got a function foo with a litany of constraints. Of course these constraints must appear in the signatures of functions that use foo, so what I'm trying to do is wrap the foo constraints in a type synonym FooCtx a b ... :: Constraint. As an example, foo ::...

Swift's pow() function won't accept Doubles as arguments

swift,generics,pow,downcasting,type-constraints
I created this infix operator ^^ as a substitute to using the pow function: infix operator ^^ { associativity left precedence 155 } func ^^ <T: IntegerLiteralConvertible>(left: T, right: T) -> T { return pow(left as Double, right as Double) } I used the IntegerLiteralConvertible protocol as a type constraint...

Can I parameterise the empty constraint type?

haskell,typeclass,type-constraints,type-families
I have a class for queues which allows the instance to define the constraints it places on the elements. For example, a priority queue requires its elements to be orderable: {-# LANGUAGE MultiParamTypeClasses, ConstraintKinds, FunctionalDependencies #-} class Queue q c | q -> c where empty :: q a qpop...

Swift - how to declare a method which receives a number in a range

swift,generics,protocols,type-constraints
I want to create a function which has a number parameter that should be between 0..100 % I thought that the best way to enforce this would be by creating a wrapper type using FloatingPointType protocol , but I am getting a compilation error Protocol 'FloatingPointType' can only be used...

Generalized type constraints with Swift

generics,swift,type-constraints,swift-generics,swift-extensions
As an exercise, I'm trying to extend Array in Swift to add a sum() member function. This should be type safe in a way that I want a call to sum() to compile only if the array holds elements that can be added up. I tried a few variants of...

How does the <:< operator work in Scala?

scala,type-constraints
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...

Constructing/making a generic type and turning a type constraint into a struct-as-base-type constraint

c#,generics,boxing,type-constraints,generic-constraints
Normally we cannot constrain a type parameter T to deriving from a sealed type (such as a struct type). This would be meaningless because there is only one type which could fit, and as such there is no need for generics. So constraints like: where T : string or: where...

How to use f# constraints for generic calculation functions?

f#,type-constraints
type Point<'t> = val X : 't val Y : 't new(x : 't,y : 't) = { X = x; Y = y } let clampedSubtract (p1:Point<_>) (p2:Point<_>) = Point( max (p2.X - p1.X) 0, max (p2.Y - p1.Y) 0 ) If you look at the code above, you...