Pim Jager
Pim Jager

Reputation: 32129

Using type variable from declaration in function

I'm quite new to Haskell and I'm trying to use a type variable from the function declaration in the creation (not quite sure what to call this in Haskell) of a record type.
As always, code explains my problem a lot better:

data S a = S {x::a}

f :: a -> S a
f n = (S a){x=n}

GHC says that a is not in scope on the last line. How could this be accomplished?

Upvotes: 1

Views: 133

Answers (2)

Alexey Romanov
Alexey Romanov

Reputation: 170899

In cases where you do need the type variable from declaration, use the scoped type variables extension.

Upvotes: 1

user1937198
user1937198

Reputation: 5390

Use f n = S {x=n}, the type variable is unnecessary

Upvotes: 5

Related Questions