One of my functions returns either:
parse :: Int -> String -> Either String JsonLikeValue
And other takes JsonLikeValue as a parameter:
convert :: Int -> JsonLikeValue -> Either InvalidState To
JsonLikeValue is custom data type:
data JsonLikeValue = JLString String | JLInt Int | JLArray [JsonLikeValue] deriving (Show, Eq)
And when I call:
convert num (parse size message)
it gives following:
Couldn't match expected type ‘JsonLikeValue’
with actual type ‘Either String JsonLikeValue’
How can this be avoided and only right side be passed as argument? Thanks in advance :)
EDIT: solution marked as answer is very helpful since it takes into consideration all possible outcomes, and as I'm not an expert but still, looks more professional, so it is very important to take a look at it.
But what was enough to solve the required extraction was:
extractValue :: Either String JsonLikeValue -> JsonLikeValue
extractValue a = case a of
Left e -> JLArray []
Right r -> r
where in case of Right it returns only value, and in case of Left it returns something of required data type. Note that this only works if you're 100% sure that argument will return Right value. Otherwise, check answer below.
Another good notice in comments is to use fromRight from Data.Either