Questions tagged [type-parameter]

A type parameter is a parameter whose value is a type.

A parameter whose value is a type. Often, type parameters are a only compile time feature of a programming language.

C++'s template type parameters provide an example of type parameters.

472 questions
38
votes
2 answers

Scala - creating a type parametrized array of specified length

If in Scala IDE try the following: val chars = Array[Char](256) it is all fine. But if I do this: val len = 256 val chars = Array[Char](len) it says that it expects a Char instead of len? Why? I expect the behavior to be the same! Why does it…
noncom
  • 4,762
  • 3
  • 34
  • 69
37
votes
2 answers

Writing Algebraic Data Type in Scala

In Haskell, I can define a Tree: data Tree a = Empty | Node a (Tree a) (Tree a) How could I write this in Scala? I'm not sure how to keep the type parameter [A] in Scala for Node to match Tree's type, a.
Kevin Meredith
  • 38,251
  • 58
  • 190
  • 340
37
votes
2 answers

What is the difference between bounded wildcard and type parameters?

Is there a difference between Collection getThatCollection(Class type) and Collection getThatCollection(Class)
rrejex
  • 371
  • 3
  • 3
37
votes
4 answers

How to instantiate an instance of type represented by type parameter in Scala

example: import scala.actors._ import Actor._ class BalanceActor[T <: Actor] extends Actor { val workers: Int = 10 private lazy val actors = new Array[T](workers) override def start() = { for (i <- 0 to (workers - 1)) { …
scaling_out
  • 1,023
  • 1
  • 10
  • 15
27
votes
3 answers

What's wrong with this reflection code? GetFields() is returning an empty array

C#, Net 2.0 Here's the code (I took out all my domain-specific stuff, and it still returns an empty array): using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace ConsoleApplication1 { class…
Chris McCall
  • 10,043
  • 8
  • 46
  • 79
24
votes
3 answers

How do I make a class generic for all Numeric Types?

I am trying to create a Vector class that is generic for all numeric types. my original attempt was to write a class for all Types like this: class Vector3f(val x:Float, val y:Float, val z:Float) since scala supports the specialised annotation I…
Arne
  • 7,206
  • 4
  • 41
  • 60
22
votes
1 answer

Why can't I use 'as' with generic type parameter that is constrained to be an interface?

In the example below (only for demo purpose), if T is not constrained with class, then this conversion: var ret = objectA as T; ..will cause the following compile error: The type parameter 'T' cannot be used with the 'as' operator because it…
ricky
  • 1,768
  • 3
  • 15
  • 40
21
votes
1 answer

Abstract types versus type parameters

In what situations should abstract types be preferred over type parameters?
Jay Sinha
  • 423
  • 5
  • 16
18
votes
1 answer

Can one reduce the number of type parameters?

I find it annoying that one has to specify the types of both Foo and FooFactory in the call to RunTest below. After all, if the test knows the type of the Factory, the type the Factory is creating is implied. Assuming I want to run a lot of…
William Jockusch
  • 26,421
  • 48
  • 170
  • 299
16
votes
3 answers

typeof(T) within generic nested types

I don't understand why the following behaves the way it does at all. I don't even know if it's caused by hiding or something else. class A { public class B : A { public void b() { …
Petr Hudeček
  • 1,422
  • 18
  • 29
15
votes
3 answers

Adding a custom view to XML... but with a GENERIC-type

I am working on a custom view with a hope of reusability. It should have a generic type, like this: public class CustomViewFlipper extends ViewFlipper { } I know how to bind a normal custom view to the XML file. But I couldn't find any…
eks
  • 481
  • 4
  • 13
15
votes
3 answers

How to write lambdas with generics in kotlin?

I can write lambdas id_Int and id_Boolean with explicit type. And I can write function identity with type parameter. Can I write lambdas with type parameter? fun testFuncInt(f: (Int) -> Int): Int = f(1) + 2 val id_Int = { x: Int -> x } fun…
xiang
  • 1,074
  • 8
  • 26
15
votes
3 answers

Build A Generic Tree With Inheritance

I am building a generic Tree class, which supports inheritance of sub-trees. But I've encountered some problems. Would you please kindly help me? Description Let's define the Tree class and the BlueTree class, where BlueTree extends Tree. Let's…
midnite
  • 4,919
  • 7
  • 34
  • 51
15
votes
5 answers

Java: getClass() of bounded type

I noticed something while I was derping around with generics. In the example below, doStuff1 compiles but doStuff2 doesn't: public void doStuff1(T value) { Class theClass = value.getClass(); } public
BambooleanLogic
  • 6,088
  • 2
  • 26
  • 51
15
votes
2 answers

Why does VS warn me that typeof(T) is never the provided type in a generic method where the type parameter is restricted to implement T?

I hope the question is correct, so let's give you an example. Imagine the following generic method: public abstract class Base : IDisposable { public static IEnumerable GetList() where T : Base { // To ensure T inherits…
Carsten
  • 10,430
  • 7
  • 37
  • 58
1
2 3
31 32