Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't i use function parameters in a record update notation?

Tags:

haskell

Example of broken code:

data Foo = Foo {
    bar :: (Int -> Int)
  }

baz = Foo { bar i = i*3 }

Why isn't this possible?

like image 592
Vektorweg Avatar asked Jun 13 '14 23:06

Vektorweg


2 Answers

It's just a syntactic limitation - I suspect that if this feature has been considered, it would have been rejected because there are straightforward alternatives. Also, if it was supported, the next question would be why not pattern-matching with multiple clauses, and overall it would just make the language bigger for not all that much gain.

You can use baz = Foo { bar = \x -> x*3 } instead for the specific case you've given, or define an auxiliary function.

like image 144
GS - Apologise to Monica Avatar answered Oct 19 '22 06:10

GS - Apologise to Monica


This should work:

baz = Foo { bar = (\x -> x*3) }
like image 3
Sibi Avatar answered Oct 19 '22 08:10

Sibi