parametric-polymorphism

How do I generically iterate over the properties of an arbitrary object in TypeScript?

对着背影说爱祢 提交于 2021-01-27 17:10:14
问题 This is a pretty common JavaScript pattern: function mapThruWrapper(module) { const replacement = {} Object.getOwnPropertyNames(module).forEach(function(key) { const val = module[key] if (val instanceof Function) { replacement[key] = wrapperFunc.bind(null, val) } else { replacement[key] = val } }) return replacement } I'm trying to strongly type this in TypeScript, and I've gotten as far as something like the following: function mapThruWrapper<M extends { [X: string]: unknown }>(module: M): M

Why does Haskell's `head` crash on an empty list (or why *doesn't* it return an empty list)? (Language philosophy)

折月煮酒 提交于 2020-01-28 13:20:25
问题 Note to other potential contributors: Please don't hesitate to use abstract or mathematical notations to make your point. If I find your answer unclear, I will ask for elucidation, but otherwise feel free to express yourself in a comfortable fashion. To be clear: I am not looking for a "safe" head , nor is the choice of head in particular exceptionally meaningful. The meat of the question follows the discussion of head and head' , which serve to provide context. I've been hacking away with

Parametric Polymorphism vs Subtype polymorphism F#

风格不统一 提交于 2020-01-14 01:44:43
问题 What is the difference (if any) between these two F# type signatures? UseTheStream<'a when 'a :> Stream> : 'a -> unit and UseTheStream : (stream : Stream) -> unit Do they mean the same thing in this case? msdn says the following about the (:>) Type Constraint type-parameter :> type -- The provided type must be equal to or derived from the type specified, or, if the type is an interface, the provided type must implement the interface. This would indicate that the two signatures are saying the

How can classes be made parametric in Perl 6?

旧街凉风 提交于 2020-01-12 15:46:05
问题 Normally in Perl 6, only roles are allowed to be parametric. Here, we'll be attempting to make classes, a kind (referred to from here on out as a metaobject) that isn't normally allowed to be parametric, parametric. If you try to make a class parametric the naive way, this happens: bastille% perl6 -e 'class Foo[::T] {}' ===SORRY!=== Error while compiling -e Unable to parse class definition at -e:1 ------> class Foo⏏[::T] {} expecting any of: generic role But if you take a look at what

Using a context bound in a class type parameter

戏子无情 提交于 2020-01-11 09:15:02
问题 I was under the impression that context bounds would work only on methods: trait Target[T] class Post { def pinTo[T : Target](t:T) } apparently context bounds can be used in class (and presumably trait ) too: trait Target[T] class Post[T:Target] { def pintTo[T](t:T) } Now I'm confused as to how the evidence can be provided to Post ? class Business implicit object ev extends Target[Business] // is implicit necessary here ? val p = new Post[Business] // ?? how do I provide ev ? related to

Using a context bound in a class type parameter

邮差的信 提交于 2020-01-11 09:14:08
问题 I was under the impression that context bounds would work only on methods: trait Target[T] class Post { def pinTo[T : Target](t:T) } apparently context bounds can be used in class (and presumably trait ) too: trait Target[T] class Post[T:Target] { def pintTo[T](t:T) } Now I'm confused as to how the evidence can be provided to Post ? class Business implicit object ev extends Target[Business] // is implicit necessary here ? val p = new Post[Business] // ?? how do I provide ev ? related to

Why does `peek` with a polymorphic Ptr return GHC.Prim.Any when used with a bind?

老子叫甜甜 提交于 2020-01-02 08:38:25
问题 Using the low-level GNU Science Library bindings Bindings.Gsl.RandomNumberGeneration, I'm running into this odd type behavior in GHCi where binding changes return type from a peek into GHC.Prim.Any . I'm trying to understand why since I can't use the c'rng_alloc unless I retain the type of pointer to an rng . For eample: λ> :t c'gsl_rng_alloc c'gsl_rng_alloc :: Ptr C'gsl_rng_type -> IO (Ptr C'gsl_rng) λ> :t p'gsl_rng_mt19937 p'gsl_rng_mt19937 :: Ptr (Ptr gsl_rng_type) λ> :t peek p'gsl_rng

What is polymorphism in Javascript?

混江龙づ霸主 提交于 2019-12-28 04:39:06
问题 I have read some possible article I could found on the internet on polymorphism . But I think I could not quite grasp the meaning of it and its importance. Most of the articles don't say why it is important and how I can achieve polymorphic behavior in OOP (of course in JavaScript). I can not provide any code example because I haven't got the idea how to implement it, so my questions are below: What is it? Why we need it ? How it works? How can I achieve this polymorphic behavior in