Rémi Lefèvre
2017-09-28 23:16:49 UTC
Hi,
Does anyone know if there is a way to use higher-order functions with a
function using an extensible record?
When I try to build this code:
type alias Named a =
{ a | name : Maybe String }
getName : Named a -> Maybe String
getName { name } =
name
type Element
= AnElement { name : Maybe String }
| AnotherElement { name : Maybe String }
mapNamed : (Named a -> b) -> Element -> b
mapNamed func element =
case element of
AnElement e ->
func e
AnotherElement e ->
func e
getElementName : Element -> Maybe String
getElementName e =
mapNamed getName e
I get the following error:
Detected errors in 1 module. -- TYPE MISMATCH
----------------------------------- Types.elm The argument to function
`func` is causing a mismatch. 13| func e ^ Function `func` is expecting the
argument to be: Named a But it is: { name : Maybe String } Hint: Your type
annotation uses type variable `a` which means any type of value can flow
through. Your code is saying it CANNOT be anything though! Maybe change
your type annotation to be more specific? Maybe the code has a problem?
More at:
<https://github.com/elm-lang/elm-compiler/blob/0.18.0/hints/type-annotations.md>
I struggle to understand why this does not work whereas replacing *func* by
*getName* satisfies the compiler:
getElementName : Element -> Maybe String
getElementName element =
case element of
AnElement e ->
getName e
AnotherElement e ->
getName e
Any idea ?
Thank you and sorry if this has been already discussed, I did not find
anything.
Does anyone know if there is a way to use higher-order functions with a
function using an extensible record?
When I try to build this code:
type alias Named a =
{ a | name : Maybe String }
getName : Named a -> Maybe String
getName { name } =
name
type Element
= AnElement { name : Maybe String }
| AnotherElement { name : Maybe String }
mapNamed : (Named a -> b) -> Element -> b
mapNamed func element =
case element of
AnElement e ->
func e
AnotherElement e ->
func e
getElementName : Element -> Maybe String
getElementName e =
mapNamed getName e
I get the following error:
Detected errors in 1 module. -- TYPE MISMATCH
----------------------------------- Types.elm The argument to function
`func` is causing a mismatch. 13| func e ^ Function `func` is expecting the
argument to be: Named a But it is: { name : Maybe String } Hint: Your type
annotation uses type variable `a` which means any type of value can flow
through. Your code is saying it CANNOT be anything though! Maybe change
your type annotation to be more specific? Maybe the code has a problem?
More at:
<https://github.com/elm-lang/elm-compiler/blob/0.18.0/hints/type-annotations.md>
I struggle to understand why this does not work whereas replacing *func* by
*getName* satisfies the compiler:
getElementName : Element -> Maybe String
getElementName element =
case element of
AnElement e ->
getName e
AnotherElement e ->
getName e
Any idea ?
Thank you and sorry if this has been already discussed, I did not find
anything.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.