Questions tagged [type-inference]

Type inference is the process of inferring types for programs automatically, using rules defined by a type system.

type-inference
Filter by
Sorted by
Tagged with
405 votes
86 answers
149k views

Use of var keyword in C#

After discussion with colleagues regarding the use of the 'var' keyword in C# 3 I wondered what people's opinions were on the appropriate uses of type inference via var? For example I rather lazily ...
325 votes
20 answers
299k views

Checking if an object is a given type in Swift

I have an array that is made up of AnyObject. I want to iterate over it, and find all elements that are array instances. How can I check if an object is of a given type in Swift?
Encore PTL's user avatar
  • 8,134
287 votes
4 answers
228k views

Collections.emptyList() returns a List<Object>?

I'm having some trouble navigating Java's rule for inferring generic type parameters. Consider the following class, which has an optional list parameter: import java.util.Collections; import java.util....
Chris Conway's user avatar
  • 55.6k
258 votes
15 answers
62k views

How much is too much with C++11 auto keyword?

I've been using the new auto keyword available in the C++11 standard for complicated templated types which is what I believe it was designed for. But I'm also using it for things like: auto foo = std::...
Alan Turing's user avatar
  • 12.4k
222 votes
22 answers
53k views

Why is Swift compile time so slow?

I'm using Xcode 6 Beta 6. This is something that's been bugging me for some time now, but it's reaching a point where it's barely usable now. My project is starting to have a decent size of 65 Swift ...
apouche's user avatar
  • 9,883
216 votes
8 answers
234k views

What is the meaning of the auto keyword?

From what I've learned, auto has always been a weird storage class specifier that didn't serve any purpose. However, I've tried what auto does, and it assumes the type of whatever I happen to assign ...
Anne Quinn's user avatar
  • 12.8k
189 votes
5 answers
29k views

Why can't the C# constructor infer type?

Why is type inference not supported for constructors the way it is for generic methods? public class MyType<T> { private readonly T field; public MyType(T value) { field = value; } } ...
theburningmonk's user avatar
169 votes
7 answers
126k views

How to pass a class type as a function parameter

I have a generic function that calls a web service and serialize the JSON response back to an object. class func invokeService<T>(service: String, withParams params: Dictionary<String, ...
Jean-Francois Gagnon's user avatar
155 votes
14 answers
19k views

Is there a downside to declaring variables with auto in C++?

It seems that auto was a fairly significant feature to be added in C++11 that seems to follow a lot of the newer languages. As with a language like Python, I have not seen any explicit variable ...
DxAlpha's user avatar
  • 1,443
152 votes
4 answers
82k views

Why is the infer keyword needed in Typescript?

Why did the Typescript folks create the infer keyword? According to the documents, this is an example of how you would use it: type ReturnType<T> = T extends (...args: any[]) => infer R ? R :...
CodyBugstein's user avatar
  • 22.7k
129 votes
1 answer
90k views

Calling static generic methods

I have come across a curious situation involving static generic methods. This is the code: class Foo<E> { public static <E> Foo<E> createFoo() { // ... } } ...
fredoverflow's user avatar
120 votes
1 answer
3k views

What limits does scala place on the "acceptable complexity" of inferred types?

According to the Scala Language Spec: ... local type inference is permitted to limit the complexity of inferred bounds [of type parameters]. Minimality and maximality of types have to be ...
Owen's user avatar
  • 39.2k
110 votes
8 answers
30k views

How can I reliably determine the type of a variable that is declared using var at design time?

I'm working on a completion (intellisense) facility for C# in emacs. The idea is, if a user types a fragment, then asks for completion via a particular keystroke combination, the completion facility ...
107 votes
3 answers
14k views

Is Julia dynamically typed?

A lot of blogs, and the manual itself, say that Julia is dynamically typed. But from my reading of the manual, it sounds to me more like it is statically typed with type inference, like F#. Is Julia ...
Frames Catherine White's user avatar
104 votes
9 answers
107k views

Not able to "findViewById" in Kotlin. Getting error "Type inference failed"

I am getting the following error when I try to find a RecycleView by id. Error:- Type inference failed: Not enough information to infer parameter T Code: class FirstRecycleViewExample : ...
Ravindra Kushwaha's user avatar
102 votes
5 answers
14k views

implementing type inference

I see some interesting discussions here about static vs. dynamic typing. I generally prefer static typing, due to compile type checking, better documented code, etc. However, I do agree that they do ...
deepblue's user avatar
  • 8,486
92 votes
4 answers
47k views

Why does this Haskell code produce the "infinite type" error?

I am new to Haskell and facing a "cannot construct infinite type" error that I cannot make sense of. In fact, beyond that, I have not been able to find a good explanation of what this error ...
Charlie Flowers's user avatar
91 votes
9 answers
35k views

Why doesn't C# infer my generic types?

I'm having lots of Funcy fun (fun intended) with generic methods. In most cases C# type inference is smart enough to find out what generic arguments it must use on my generic methods, but now I've got ...
Steven's user avatar
  • 170k
89 votes
3 answers
7k views

A peculiar feature of exception type inference in Java 8

While writing code for another answer on this site I came across this peculiarity: static void testSneaky() { final Exception e = new Exception(); sneakyThrow(e); //no problems here ...
Marko Topolnik's user avatar
87 votes
1 answer
8k views

What is the monomorphism restriction?

I'm puzzled by how the Haskell compiler sometimes infers types that are less polymorphic than what I'd expect, for example when using point-free definitions. It seems like the issue is the "...
Bakuriu's user avatar
  • 101k
86 votes
7 answers
203k views

The type arguments for method cannot be inferred from the usage

Maybe I'm overworked, but this isn't compiling (CS0411). Why? interface ISignatur<T> { Type Type { get; } } interface IAccess<S, T> where S : ISignatur<T> { S Signature { ...
Ben's user avatar
  • 1,035
78 votes
6 answers
23k views

What is the difference between decltype and auto as a placeholder type for variables?

As I understand it, both decltype and auto will attempt to figure out what the type of something is. If we define: int foo () { return 34; } Then both declarations are legal: auto x = foo(); cout ...
James Leonard's user avatar
77 votes
3 answers
29k views

Parametric polymorphism vs Ad-hoc polymorphism

I would like to understand the key difference between parametric polymorphism such as polymorphism of generic classes/functions in the Java/Scala/C++ languages and "ad-hoc" polymorphism in ...
Ilya Lakhin's user avatar
  • 1,924
77 votes
3 answers
21k views

Why does this Java 8 program not compile?

This program compiles fine in Java 7 (or in Java 8 with -source 7), but fails to compile with Java 8: interface Iface<T> {} class Impl implements Iface<Impl> {} class Acceptor<T ...
ghik's user avatar
  • 10.8k
74 votes
8 answers
43k views

JDK 11.0.2 compilation fails with javac NPE on anonymous parameterized class type inference

Code (spring-web 5.1.2) public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders....
Mikhail Kholodkov's user avatar
68 votes
4 answers
5k views

Why is F#'s type inference so fickle?

The F# compiler appears to perform type inference in a (fairly) strict top-to-bottom, left-to-right fashion. This means you must do things like put all definitions before their use, order of file ...
J Cooper's user avatar
  • 17k
63 votes
3 answers
8k views

Why do Consumers accept lambdas with statement bodies but not expression bodies?

The following code surprisingly is compiling successfully: Consumer<String> p = ""::equals; This too: p = s -> "".equals(s); But this is fails with the error boolean cannot be converted ...
Zefick's user avatar
  • 2,109
63 votes
9 answers
44k views

TS4023: Exported Variable <x> has or is using name <y> from external module but cannot be named

I've seen this answered before, but they don't seem to cover this specific use case (or they don't work/help) import {Route} from 'vue-router'; export const detailRoute = { path: '/detail/:id', ...
user avatar
62 votes
6 answers
18k views

How to show inferred TypeScript type in WebStorm?

In VS2012, you can hover over a typescript variable and it will show you the inferred type. Is there a similar feature in webstorm?
Joe's user avatar
  • 11.4k
62 votes
4 answers
74k views

Stop Pandas from converting int to float due to an insertion in another column

I have a DataFrame with two columns: a column of int and a column of str. I understand that if I insert NaN into the int column, Pandas will convert all the int into float because there is no NaN ...
user2570465's user avatar
  • 2,487
62 votes
1 answer
1k views

Problem understanding C# type inference as described in the language specification

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&...
Timwi's user avatar
  • 66k
59 votes
4 answers
52k views

Why does using Collections.emptySet() with generics work in assignment but not as a method parameter?

So, I have a class with a constructor like this: public FilterList(Set<Integer> labels) { ... } and I want to construct a new FilterList object with an empty set. Following Joshua Bloch's ...
Karl von L's user avatar
  • 1,601
59 votes
7 answers
8k views

Generic methods in .NET cannot have their return types inferred. Why?

Given: static TDest Gimme<TSource,TDest>(TSource source) { return default(TDest); } Why can't I do: string dest = Gimme(5); without getting the compiler error: error CS0411: The type ...
Steve Dunn's user avatar
  • 21.4k
57 votes
3 answers
3k views

Named arguments and generic type inference in C# 4.0

I had been programming under the assumption that, when calling a method in C# 4.0, supplying names for your arguments would not affect the outcome unless in doing so you were "skipping" one or more ...
BirgerH's user avatar
  • 748
54 votes
11 answers
6k views

Type-inferring a constant in C#

In C#, the following type-inference works: var s = "abcd"; But why can't the type be inferred when the variable is a constant? The following throws a compile-time exception: const var s = "abcd"; /...
Andreas Grech's user avatar
53 votes
5 answers
55k views

Scala: How to define "generic" function parameters?

I am trying to learn Scala now, with a little bit of experience in Haskell. One thing that stood out as odd to me is that all function parameters in Scala must be annotated with a type - something ...
airportyh's user avatar
  • 22.3k
53 votes
3 answers
31k views

Does Java 10 provide a val keyword? If not, why not?

Java 10 brings a C#-like var keyword for local type-inference. But does Java 10 also provide a val keyword, as is found in Scala? val would work like var but the binding would be final. var x = "...
sdgfsdh's user avatar
  • 35.5k
53 votes
3 answers
5k views

What does decltype((...)) with double parentheses mean? [duplicate]

I don't understand the last line of the example on page 148 of the FCD (§7.6.1.2/4): const int&& foo(); int i; struct A { double x; }; const A* a = new A(); decltype(foo()) x1 = i; // ...
fredoverflow's user avatar
50 votes
1 answer
712 views

Unexpected implicit resolution based on inference from return type

Given a typeclass where instance selection should be performed based on the return type: case class Monoid[A](m0: A) // We only care about the zero here implicit def s[T] : Monoid[Set[T]] = Monoid(...
Patrick Prémont's user avatar
48 votes
5 answers
20k views

Functions with generic parameter types

I am trying to figure out how to define a function that works on multiple types of parameters (e.g. int and int64). As I understand it, function overloading is not possible in F# (certainly the ...
Noldorin's user avatar
  • 146k
47 votes
5 answers
6k views

Why is Scala's type inference not as powerful as Haskell's?

The type inference engine of Haskell is much more powerful than Scala's. In Haskell I rarely have to explicitly write the types whereas in Scala the types can only be inferred in expressions but not ...
kiritsuku's user avatar
  • 53.1k
47 votes
2 answers
36k views

OCaml explicit type signatures

In Haskell, it is considered good practice to explicitly declare the type signature of your functions, even though it can (usually) be inferred. It seems like this isn't even possible in OCaml, e.g. ...
Xodarap's user avatar
  • 11.8k
45 votes
7 answers
49k views

C# Type suffix for decimal

I don't know what the correct wording is for what I am trying to achieve so it may already be posted online. Please be kind if it is. Ok so basically I have this method. public static T IsNull<T&...
Maxim Gershkovich's user avatar
42 votes
4 answers
46k views

Go fails to infer type in assignment: "non-name on left side of :="

This snippet works as expected play.golang.org/p/VuCl-OKMav i := 10 next := 11 prev, i := i, next However this nearly identical snippet gives non-name f.Bar on left side of := play.golang.org/p/...
deft_code's user avatar
  • 58.2k
42 votes
8 answers
3k views

How do I define Lisp’s apply in Haskell?

Shouldn’t this definition be allowed in a lazy language like Haskell in which functions are curried? apply f [] = f apply f (x:xs) = apply (f x) xs It’s basically a function that applies the given ...
is7s's user avatar
  • 3,500
41 votes
1 answer
19k views

Scala String vs java.lang.String - type inference

In the REPL, I define a function. Note the return type. scala> def next(i: List[String]) = i.map {"0" + _} ::: i.reverse.map {"1" + _} next: (i: List[String])List[java.lang.String] And if I ...
Luigi Plinge's user avatar
  • 50.9k
41 votes
3 answers
62k views

What's the difference between dtype and converters in pandas.read_csv?

pandas function read_csv() reads a .csv file. Its documentation is here According to documentation, we know: dtype : Type name or dict of column -> type, default None Data type for data or ...
Bryan's user avatar
  • 1,507
40 votes
3 answers
2k views

Java 10: Will Java 7's Diamond Inference Work with Local Type Inference?

From JEP 286, we see that we'll be able to utilize local type inference (var) in JDK 10 (18.3). The JEP states that the following compiles, which is expected: var list = new ArrayList<String>()...
Jacob G.'s user avatar
  • 29.3k
40 votes
5 answers
6k views

How to write a function for generic numbers?

I'm quite new to F# and find type inference really is a cool thing. But currently it seems that it also may lead to code duplication, which is not a cool thing. I want to sum the digits of a number ...
primfaktor's user avatar
  • 2,945
40 votes
6 answers
10k views

Why is this type inference not working with this Lambda expression scenario?

I have a weird scenario where type inference isn't working as I'd expect when using a lambda expression. Here's an approximation of my real scenario: static class Value<T> { } @...
Josh Stone's user avatar
  • 4,388

1
2 3 4 5
55