Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can classes be used as type parameters and what for?

I've accidentally discovered that the following is a perfectly compiling code:

class SomeClass a

someValue :: Maybe (SomeClass a)
someValue = undefined

I'm only used to seeing classes in type constraints, so I don't understand why this code typechecks and what's the purpose of using classes that way.

I'm on GHC 7.6.2.

like image 283
Nikita Volkov Avatar asked Apr 09 '13 13:04

Nikita Volkov


People also ask

What is the type parameter of a class?

A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

What are the types of parameters in Java?

Parameter Types We can use any data type such as primitive data types including int, float, double, char, short, byte, String, and object reference variables for a parameter of a method and constructor. There is no standard limit to specify the number of parameters in the definition of a method.

What are valid names of a type parameter specified for a class or a method?

Type Parameter Naming Conventions The most commonly used type parameter names are: E - Element (used extensively by the Java Collections Framework) K - Key. N - Number.

What are type parameters in C#?

The type parameter is a placeholder for a specific type that the client specifies when they create an instance of the generic type. A generic class cannot be used as-is because it is simply a blueprint for that type.


2 Answers

It's a bug in ghc-7.6.2. It should be a kind error. Please report it.

like image 152
augustss Avatar answered Oct 22 '22 14:10

augustss


Error.

A.hs:3:21: Class `SomeClass' used as a type

In the type signature for `someValue':
like image 32
Don Stewart Avatar answered Oct 22 '22 16:10

Don Stewart