Questions tagged [parameterized-types]

72 questions
21
votes
5 answers

Why is it possible to get back an object of "incorrect-type" from the parametrized List in Java?

Here's a code snippet: import java.util.*; class Test { public static void main(String[] args) { List list = new ArrayList<>(); addToList(list); Integer i = list.get(0); //#1 fails at run-time String…
fromSPb
  • 313
  • 2
  • 7
12
votes
3 answers

flow generic type for function expression (arrow functions)

I usually try to keep flow function types separate from their implementation. It's a slightly more readable when I write: type Fn = string => string; const aFn: Fn = name => `hello, ${ name }`; rather than: const aFn = (name: string): string =>…
Sam R.
  • 14,850
  • 9
  • 56
  • 106
12
votes
3 answers

How do parameterized methods resolve if it's not an input parameter?

How are references to << T >> handled by the compiler in the following code, since the method takes no parameters that would allow inference of T? Are any restrictions being placed on what type of object can be placed into the list? Is a cast even…
swingMan
  • 722
  • 6
  • 15
11
votes
1 answer

Why is anonymous class required in "super type token" pattern in java

In Neal Gafter's "super type token" pattern (http://gafter.blogspot.com/2006/12/super-type-tokens.html), an anonymous object was used to pass in the parameterized type : class ReferenceType{} /* anonymous subclass of "ReferenceType"…
superluli
  • 113
  • 9
8
votes
2 answers

Data type parametrized by constant in Haskell

I would like to define a data type in Haskell which is parametrized by an Int constant along the lines: data Q (n :: Int) = Q n (Int,Int) -- non-working code in order to allow me to define functions of the type: addQ :: (Q n)->(Q n)->(Q n) addQ (Q k…
8
votes
1 answer

Why does the java compiler give "rawtypes" warning for class literal?

I'm using mockito to write some tests, and I'm using the following bit of code: ArgumentCaptor captor = ArgumentCaptor.forClass(LinkedList.class); This compiles and runs just fine, except for the warning that "captor" is raw type and I…
jwepurchase
  • 675
  • 5
  • 20
5
votes
1 answer

Understanding bounded generics in java. What is the point?

I am trying to understand bounded types and not quite grasping the point of them. There is an example of bounded generics on which provides this use case: public class NaturalNumber { private T n; public NaturalNumber(T…
Stephen
  • 3,350
  • 13
  • 34
4
votes
1 answer

How can I instance `Functor` for a type with two parameters?

Background. In one of my classes we've been exploring the Parser monad. The Parser monad is typically defined as either newtype Parser a = Parser (String -> [(a, String)]) Or as newtype Parser a = Parser (String -> Maybe (a, String)) In either…
4
votes
1 answer

Java Optional Parametrized Interfaces

I have an interface like so: public interface Foo { default void doStuff(T val1, T val2) {} default void doStuff(T val) {} } The idea behind the interface being you can implement either one or the other methods. So then I have an…
Richard
  • 5,010
  • 24
  • 106
  • 182
4
votes
1 answer

Scala generics Confusion

I have some generic/OOP Scala code (not useful, just classroom excercises) I have a Container interface, "IterableContainer", that takes in and returns objects whose type is a subclass of AnyRef. It has a concrete sub-class that also takes in and…
Michael Lafayette
  • 2,710
  • 1
  • 14
  • 41
4
votes
2 answers

Java generics - parameterized class vs. typed method

I guess this is a duplicated question, but after browsing heaps of related questions, I couldn't find a matching one ... yeah, lame excuse ;) I'm currently developing a common interface for POIs HSLF/XSLF implementations. The reason for using…
kiwiwings
  • 3,259
  • 1
  • 20
  • 57
4
votes
1 answer

Avoid Unchecked Casting Warnings in Parameterized Type Casting

Is it possible to avoid the unchecked warning when typecasting a parameterized type object? For example, below is the actual situation I face, var1 is of type JComboBox I wish to store it in a Map and then retrieve it forcing me into the…
Lordbalmon
  • 1,480
  • 1
  • 12
  • 27
4
votes
1 answer

Beginning Haskell: A project based....How to I construct a Vector from a (Double, Double)

I'm working through the examples in "Beginning Haskell: A project based approach" and in Chapter 6 you write kMeans during which I pass in a [(Double,Double)] and convert them to vectors and life is good. It all works. However, I'd like to create a…
Tim Perry
  • 2,846
  • 1
  • 21
  • 36
4
votes
2 answers

Storing different parameterized types in the same generic arraylist in Java

I like to create a class representation of database tables in java. A column is designed as a generic class so that it can handle all different datatypes table columns can possible have. public class TableColumn { ... } A table has 0 ... n…
John Tuck
  • 63
  • 5
4
votes
2 answers

What is the use case for the constructor constraint in Delphi?

The title is pretty much it... Why would you ever want to use the constructor constraint? It's clearly implied by the class constraint. If you use it alone, you can't do anything with the thing you've created. Why does it even exist?…
Nick Hodges
  • 15,957
  • 9
  • 59
  • 120
1
2 3 4 5