parametric-polymorphism

What are type quantifiers?

和自甴很熟 提交于 2019-12-02 22:20:59
Many statically typed languages have parametric polymorphism. For example in C# one can define: T Foo<T>(T x){ return x; } In a call site you can do: int y = Foo<int>(3); These types are also sometimes written like this: Foo :: forall T. T -> T I have heard people say "forall is like lambda-abstraction at the type level". So Foo is a function that takes a type (for example int), and produces a value (for example a function of type int -> int). Many languages infer the type parameter, so that you can write Foo(3) instead of Foo<int>(3) . Suppose we have an object f of type forall T. T -> T .

Using a context bound in a class type parameter

岁酱吖の 提交于 2019-12-01 17:50:58
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 Modeling a binary relationship between two types The A: Foo notation for context bounds is only a shortcut

Type signature in a where clause

一个人想着一个人 提交于 2019-11-30 08:06:12
问题 I've written a function similar to Data.Enumerator.List.map that makes an Iteratee compatible with an Enumerator that feeds a different Stream type. import Data.Enumerator test :: Monad m => (ao -> ai) -> Iteratee ai m b -> Iteratee ao m b test f iter = go $$ iter where go (Continue k) = continue $ \stream -> go $$ k (fmap f stream) go (Yield res _) = yield res EOF If I omit the type signature for go , this will work just fine. However, I'd like to include it but I'm unable to determine what

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

邮差的信 提交于 2019-11-29 18:59:16
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 Haskell for a few months now (to the point that it has become my main language), but I am admittedly not

Type signature in a where clause

拈花ヽ惹草 提交于 2019-11-29 06:00:35
I've written a function similar to Data.Enumerator.List.map that makes an Iteratee compatible with an Enumerator that feeds a different Stream type. import Data.Enumerator test :: Monad m => (ao -> ai) -> Iteratee ai m b -> Iteratee ao m b test f iter = go $$ iter where go (Continue k) = continue $ \stream -> go $$ k (fmap f stream) go (Yield res _) = yield res EOF If I omit the type signature for go , this will work just fine. However, I'd like to include it but I'm unable to determine what the correct signature should be. Here's what I think it should be: go :: Monad m => Step ai m b ->

What is polymorphism in Javascript?

守給你的承諾、 提交于 2019-11-27 16:46:36
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 javascript? I have got this example. But it is easily understandable what will be outcome of this code. It doesn