How to construct a value by given constructor name and constructor arguments. For example, having the following data type
{-# LANGUAGE DeriveGeneric #-}
data Test = Foo | Bar Int | Baz String Int deriving (Generic, Show)
I would have such function
make "Foo" -- > Foo
make "Bar" 1 -- > Bar 1
make "Baz" "hi" 777 -- > Baz "hi" 777
The solution in this article is pretty close, but it works for data types only with single data constructor.
EDIT
So why do I want to do this? I have a BERT-encoded data that comes from the client. I can decode it to the Terms from the bert package. I want to have something like this
case x of
Foo -> do something
Bar x -> do something with x
and so on instead of
case x of
TupleTerm y ->
case y of
[AtomTerm "foo"] -> do something
[AtomTerm "bar", IntTerm x] -> do something with x
EDIT2
I concluded, that I was going in the wrong way.
makefunction be?String -> Test,String -> Int -> Test?String -> String -> Int -> Test?make some_stringbe? Since it depends on the value ofsome_string, it has no specific value, hence type contracts could no longer get satisfied.Terms every time.transform :: (Generic t, FromBert (Rep t)) => Term -> Maybe t