type-constraints

Using types to model arbitrary constraints for compile-time checking

喜夏-厌秋 提交于 2020-01-03 17:50:36
问题 Given the strong type system of Scala, I had an ambitious project which I'm about to abandon now because the effort to usefulness ratio seems to be too high. Basically I have some graph elements ( GE ) and they correspond to sound processes which are carried out at a given calculation rate . Graph elements are composed from other graph elements forming their inputs. Now there are rather arbitrary constraints on the inputs' rates. In the source language (SuperCollider) the rates are checked at

Returning something from another type class B in function of type class A in Haskell

强颜欢笑 提交于 2020-01-02 05:39:11
问题 I'm doing a fun project in which I'm trying to redo some basic data types and concepts from Java. Currently I'm tackling Iterators. My approach is the following: (1) Translate Interfaces to Typeclasses (2) Declare custom data types and instances for actual implementations So I created the following type classes: class Iterator it where next :: it e -> (it e, e) hasNext :: it e -> Bool class Iterable i where iterator :: Iterator it => i e -> it e class Iterable c => Collection c where add :: c

Haskell type family instance with type constraints

女生的网名这么多〃 提交于 2020-01-02 02:33:11
问题 I am trying to represent expressions with type families, but I cannot seem to figure out how to write the constraints that I want, and I'm starting to feel like it's just not possible. Here is my code: class Evaluable c where type Return c :: * evaluate :: c -> Return c data Negate n = Negate n instance (Evaluable n, Return n ~ Int) => Evaluable (Negate n) where type Return (Negate n) = Return n evaluate (Negate n) = negate (evaluate n) This all compiles fine, but it doesn't express exactly

Can you make an instance of a class not for a type but for a whole class in Haskell?

你说的曾经没有我的故事 提交于 2020-01-01 09:04:13
问题 Suppose I want to make all numbers an instance of Monoid . Instead of having to create an instance for each Num like this: instance Monoid Int where mappend = (+) mempty = 0 instance Monoid Float where mappend = (+) mempty = 0.0 -- etc Is there something like this? instance Num t => Monoid t where mappend = (+) mempty = 0 Edit Some are answering with GHC extensions and warning about the potential issues; I found that informative, but I think I will stick with Sum , Product and whatever coerce

Why Do I need to redeclare type constraint in generic subclass

戏子无情 提交于 2020-01-01 01:51:13
问题 Recently I tried to create a generic subclass by implementing a generic interface. public interface IModule<T> where T : DataBean { ..... } public class Module<T> : IModule<T> where T : DataBean { .... } It seems I can't rely on any of T's restrictions as were defined in the base interface, and I need to re-declare them myself. MSDN just provided: When using the subclass generic type parameters, you must repeat any constraints stipulated at the base class level at the subclass level. For

Reflexive type parameter constraints: X<T> where T : X<T> ‒ any simpler alternatives?

守給你的承諾、 提交于 2019-12-30 00:54:11
问题 Every so often I am making a simple interface more complicated by adding a self-referencing ("reflexive") type parameter constraint to it. For example, I might turn this: interface ICloneable { ICloneable Clone(); } class Sheep : ICloneable { ICloneable Clone() { … } } //^^^^^^^^^^ Sheep dolly = new Sheep().Clone() as Sheep; //^^^^^^^^ into: interface ICloneable<TImpl> where TImpl : ICloneable<TImpl> { TImpl Clone(); } class Sheep : ICloneable<Sheep> { Sheep Clone() { … } } //^^^^^ Sheep

C# generic “where constraint” with “any generic type” definition?

社会主义新天地 提交于 2019-12-27 13:36:35
问题 Let me give example: I have some generic class/interface definition: interface IGenericCar< T > {...} I have another class/interface that I want to relate with class above, for example: interface IGarrage< TCar > : where TCar: IGenericCar< (**any type here**) > {...} Basically, I want my generic IGarrage to be dependent on IGenericCar , regardless if it's IGenericCar<int> or IGenericCar<System.Color> , because I don't have any dependency to that type. 回答1: There are typically 2 ways to

How to conciliate / constraint types between two separated expressions

耗尽温柔 提交于 2019-12-24 13:38:35
问题 I'm playing with haskell-distributed (Cloud Haskell) and I cannot make use of type constraint using returning type (must be as ... -> Process () ). To simplify context (and give a reproducible example), let -- Request / Response data ReqRes a b = Req a | Res b deriving (Read, Show) and one generic function mapping generic processes compute :: (Read a, Read b, Show a, Show b) => (a -> b) -> IO () compute f = do req <- readLn case req of Res _ -> error "Req expected" Req a -> print $ Res $ f a

How to make a generic add operator in TypeScript that works with numbers and strings

試著忘記壹切 提交于 2019-12-24 05:57:22
问题 While learning about generics in TypeScript, I wanted to try to recreate the following JavaScript: function add(x, y){ return x + y; } I tried like: type StringOrNumber = string | number; function add<MyType extends StringOrNumber>(x: MyType, y: MyType): MyType { return x + y; } This errors with: error TS2365: Operator '+' cannot be applied to types 'MyType' and 'MyType'. Why doesn't this work? I'd assume that MyType could be either a string or a number, and once "chosen" TypeScript would

F# missing type constraint

末鹿安然 提交于 2019-12-24 04:38:08
问题 In the following code, note the type constraint for get_Zero: type Wrapper<'t> = { Data : 't[] } let compute<'t when 't : (static member get_Zero : unit -> 't) and 't : (static member (~-) : 't -> 't) and 't : (static member (+) : 't * 't -> 't)> (wrapper : Wrapper<'t>) = wrapper.Data |> Seq.mapi (fun i value -> (i, value)) |> Seq.sumBy (fun (i, value) -> if i % 2 = 0 then value else -value) Even though I already have an explicit type constraint, I'm still getting the following compiler error