type-constraints

F# Generic constraint to have one generic type inherit from another

半世苍凉 提交于 2021-01-27 20:10:04
问题 In C# it is straightforward to define a generic class when one generic parameter inherits from another, e.g.: public class MyClass<TClass, TInterface> where TClass : class, TInterface { } This is used to "force" the class TClass to implement the interface TInterface . I want to do the same in F# and surprisingly it does not seem to work. For example, the following code: type Startup<'S, 'I when 'I : not struct and 'S : not struct and 'S :> 'I>() = member _.x = 0 results in FS0663 - This type

Is it possible to enforce a type constraint on a class instance for a higher-kinded type?

人盡茶涼 提交于 2021-01-27 15:00:19
问题 I have a type defined like this: newtype PrimeSet a = P Integer deriving Eq I have also defined a function which converts a prime set to a list, given that its type parameter is an Integral . toList :: Integral a => PrimeSet a -> [a] I now what to give PrimeSet a Foldable instance, so this was my first attempt (after importing fold from Data.Foldable ): instance Foldable PrimeSet where foldMap f = fold . map f . toList This didn't work, however, and the compiler told me that it Could not

Is it possible to have multiple type constaints on a generic type?

只愿长相守 提交于 2021-01-06 04:29:08
问题 In dart you can do class Preference<T extends int> to define a type constraint. But is there a way to define multiple constrains? I tried class Preference<T extends int, String> But an error get's thrown when I try to pass a argument of type T to a function that excepts a String saying The argument type 'T' can't be assigned to the parameter type 'String' 回答1: No, Dart type parameters can only have a single constraint. There is no workaround. 来源: https://stackoverflow.com/questions/61820181

Terraform - Create type constraints for type Map

倾然丶 夕夏残阳落幕 提交于 2020-12-13 03:13:35
问题 What would be the correct way to create type constraints for type map? This doesn't seem valid. variable "vpc_subnets" { type = map( key = {name = string, cidr_block = string, map_public_ip_on_launch = bool, availability_zone = string} ) } Here is what the map looks like.. vpc_subnets = { "public_subnet_a" = {name = "public_test_a", cidr_block = "10.0.0.0/28", map_public_ip_on_launch = true, availability_zone = "ap-south-1a"}, "public_subnet_b" = {name = "public_test_b", cidr_block = "10.0.0

In Haxe, can you write a generic interface where a method type parameter is constrained by the class's type parameter?

♀尐吖头ヾ 提交于 2020-05-30 03:54:29
问题 I'm having trouble writing the generic interface below. In my class, I have a function that takes an array of < any type that extends a parent class > and traces its first element. Since I'm only reading elements from the array, I'm using it as if its a covariant compound type, and therefore I'm guaranteed to have the cast statement never fail. Now I want to abstract this out even more and write a interface that defines fn using another generic type T. I want fn to be able to accept any Array

In Haxe, can you write a generic interface where a method type parameter is constrained by the class's type parameter?

♀尐吖头ヾ 提交于 2020-05-30 03:53:12
问题 I'm having trouble writing the generic interface below. In my class, I have a function that takes an array of < any type that extends a parent class > and traces its first element. Since I'm only reading elements from the array, I'm using it as if its a covariant compound type, and therefore I'm guaranteed to have the cast statement never fail. Now I want to abstract this out even more and write a interface that defines fn using another generic type T. I want fn to be able to accept any Array

Java generic method constraints - exclude type from constraint to prevent erasure problems?

∥☆過路亽.° 提交于 2020-02-02 10:14:08
问题 I am trying to overload a method based on type constraints. The code looks something like this: protected static <T extends ComponentTemplate> void addComponent(List<ComponentTemplate> factors, T component) { ... } protected static <T extends ComponentTemplate & ConditionalComponent> void addComponent(List<ComponentTemplate> factors, T conditionalComponent) { .... } ComponentTemplate is an abstract class, and ConditionalComponent is an interface. These two methods can add a component to a

Java generic method constraints - exclude type from constraint to prevent erasure problems?

这一生的挚爱 提交于 2020-02-02 10:10:11
问题 I am trying to overload a method based on type constraints. The code looks something like this: protected static <T extends ComponentTemplate> void addComponent(List<ComponentTemplate> factors, T component) { ... } protected static <T extends ComponentTemplate & ConditionalComponent> void addComponent(List<ComponentTemplate> factors, T conditionalComponent) { .... } ComponentTemplate is an abstract class, and ConditionalComponent is an interface. These two methods can add a component to a

Equality on constraints

守給你的承諾、 提交于 2020-01-24 02:55:27
问题 Basically, given {-# LANGUAGE PolymorphicKinds, ConstraintKinds, TypeFamilies #-} (and more, if necessary), does the (~) type-level operator work on type-level expressions of kind Constraint ? I tried googling the answer, but had no luck. 回答1: Yes, it is possible. Because types of kind Constraint are finite sets of atomic type constraints, you can test their equality very easily. The PolyKinds extension is not necessary, however. Also, there's very few situations when this kind equality would

VS Designer error: GenericArguments[0], 'X' on 'Y' violates the constraint of type parameter 'Z'

佐手、 提交于 2020-01-14 08:08:12
问题 I am trying to create forms that inherit from a "generic" base class where the generic argument of that base class has a constraint that it must implement one of my interfaces. It compiles and runs just fine. My problem is with the Visual Studio designer. It will open the form fine until I rebuild the project, after which it then reports the error below when trying to view the form designer until such time as I either restart Visual Studio, or remove the custom interface constraint in the