I have this class in Scala:
object Util {
class Tapper[A](tapMe: A) {
def tap(f: A => Unit): A = {
f(tapMe)
tapMe
}
def tap(fs: (A => Unit)*): A = {
fs.foreach(_(tapMe))
tapMe
}
}
implicit def tapper[A](toTap: A): Tapper[A] = new Tapper(toTap)
}
Now,
"aaa".tap(_.trim)
doesn't compile,...
I was playing around with ListW.<^>, the definition of which is as follows:
def <^>[B: Zero](f: NonEmptyList[A] => B): B = value match {
case Nil => ∅
case h :: t => f(Scalaz.nel(h, t))
}
I cannot figure out how come Option is being chosen as the Zero type for this example
scala> case class CC(v : Int)
defined class CC
scala> va...
read is defined in the Prelude as
read :: (Read a) => String -> a
and can be used as e.g. read "1" :: Int.
Now a function
readOne :: (Read a) => [String] -> (a, [String])
readOne (x:xs) = (read x,xs)
used with readOne ["1","foo"] results (as expected) in the error
Ambiguous type variable 'a' in the constraint:
'Read a' arising...
I'm working on an experimental programming language that has global polymorphic type inference.
I recently got the algorithm working sufficiently well to correctly type the bits of sample code I'm throwing at it. I'm now looking for something more complex that will exercise the edge cases.
Can anyone point me at a source of really gnar...
I'm writing some code to translate a C++ type to an appropriate type for a SQL DB. I want to identify the type, and then depending on what it is, produce the appropriate SQL code. I'm not sure exactly what can be done in this regard by using RTTI, auto, or decltype. I have some ideas but I'm not sure if they're workable.
For instance...
The JLS mentions in the type inference algorithm (§15.12.2):
It is possible that the process above yields an infinite type. This is permissible,
and Java compilers must recognize such situations and represent them appropriately using cyclic data structures.
However, I'm unable to find an actual example where javac produces an inf...
Given the following code:
type MyType() =
static member processString (_string:string) = _string.Substring(0, 1)
static member processInt (_int:int) = _int.ToString()
static member processItems = List.map MyType.processString
static member processItems = List.map MyType.processInt
The last two lines will not work. I h...
List("This","is","Scala").foreach(a => print(a+" "))
compiles fine, but
List("This","is","Scala").foreach(print(_+" "))
fails complaining of missing parameter type. I couldn't figure out why it fails.
EDIT: I meant print not println - not that it makes logical difference.
...
I'm trying to write a method in F# that returns a new instance of a generic type based upon the type of a value passed into the method. In FSI:
open System.Collections.Generic
type AttributeIndex<'a>() =
inherit SortedDictionary<'a, HashSet<int array>>()
let getNewIndexForValue (value: obj) : AttributeIndex<_> =
match valu...
EDIT: updated the question after PostMan pointed out the error on my part
Just out of curiosity, does anybody know why type inference is not supported for constructor the way they are for generic methods? i.e.
public class MyType<T>
{
private readonly T field;
public MyType(T value) { field = value; }
}
var obj = new MyType(42);...
I'm looking for information about the well-known Damas-Hindley-Milner algorithm
to do type inference for functional languages, especially information about implementation.
I already know how to do the W-algorithm, but I heard about recent new algorithms based on constraint generator/solver rather than usual unification. However, I can't...
The C# language specification describes type inference in Section §7.5.2. There is a detail in it that I don’t understand. Consider the following case:
// declaration
void Method<T>(T obj, Func<string, T> func);
// call
Method("obj", s => (object) s);
Both the Microsoft and Mono C# compilers correctly infer T = object, but my underst...
Hi all,
I'm a beginner in Scala and I'm just curious about how Scala handles the type inference for this code snippet
trait Expression { .... }
def eval (binding : String => Boolean) : Expression => Boolean
I understand that binding is a function that converts String to Boolean, but why binding at the same time could be declared as ...
Possible Duplicate:
Why can't the C# constructor infer type?
Why is the following true:
var foo = new KeyValuePair(3,4); //doesn't compile!
var boo = new KeyValuePair<int,int>(3,4); //works fine!
I would think both lines would be legal, since the type can be (should be) inferred from the parameters. Explanation?
...
Reading Disadvantages of Scala type system versus Haskell?, I have to ask: what is it, specifically, that makes Haskell's type system more powerful than other languages' type systems (C, C++, Java). Apparently, even Scala can't perform some of the same powers as Haskell's type system. What is it, specifically, that makes Haskell's type s...
I wrote a code that looks somewhat like this:
Thread t = new Thread(() => createSomething(dt, start, finish) );
t.Start();
and it works (sometimes it almost feel like there are multiple threads)
yet I don't use any delegates.
What is the meaning of a tread without a delegate
If a delegate is necessary - then please tell me what and...
I've noticed some examples of things that work and don't work when dealing with lambda functions and anonymous delegates in C#. What's going on here?
class Test : Control {
void testInvoke() {
// The best overloaded method match for 'Invoke' has some invalid arguments
Invoke(doSomething);
// Cannot convert ...
Let's consider the following function:
def printPfType[T](pf:PartialFunction[T, _])(implicit m:Manifest[T]) = {
println(m.toString)
}
Then I define the following test class:
case class Test(s:String, i:Int)
I can't write this:
printPfType {
case Test(_,i) => i
}
because the compiler can't infer the first parametric type of t...
Problem Summary
At the moment when using f# I must explicitly coerce a value to the parent type of its type in order to get pattern matching expressions to type check correctly. I would ideally like a neater way of doing.
Example
Suppose I have some class hierachy:
type Foo () =
abstract member Value : unit -> string
type A (i...
I've been thinking about how type inference works in the following OCaml program:
let rec f x = (g x) + 5
and g x = f (x + 5);;
Granted, the program is quite useless (looping forever), but what about the types?
OCaml says:
val f : int -> int = <fun>
val g : int -> int = <fun>
This would exactly be my intuition, but how does the typ...