I'm trying to get the ComboBox widget value with this code:
selectRadioB cb = do
ntxt <- comboBoxGetActiveText cb
case ntxt of
Just ("Option 1") -> ...
Just ("Option 2") -> ...
Just ("Option 3") -> ...
return ()
But, when i compile it, this error appears:
Couldn't match type ‘[Char]’
with ‘text-1.2.2.0:Data.Text.Internal.Text’
Expected type: ComboBoxText
Actual type: [Char]
In the pattern: "Option 1"
In the pattern: Just ("Option 1")
In a case alternative:
Just ("Option 1") -> ...
How can i solve that? I've made a similay question (Create a ComboBox widget in Gtk2HS), but i dont want to use the pack fuction.
Why i cant use fromString like this?
cb <- comboBoxNewText
comboBoxAppendText cb (fromString "Option 1")
comboBoxAppendText cb (fromString "Option 2")
comboBoxAppendText cb (fromString "Option 3")
comboBoxSetActive cb 0
Thanks in advance.
If we look at the documentation, we see that
ComboBoxTextis a alias forText:Now
TextandStringare both ways to represent text, butTextdoes this in a compact way, whereasString(which is actually an alias for[Char]) does this in a linked list approach (this consumes more memory and will usually result in less efficient processing).Using
unpackYou can however simply use
pack :: String -> Textandunpack :: Text -> Stringto convert betweenStringandText:So you could use:
Work with
OverloadedStringsNevertheless this is quite inefficient: you will first convert the
Textto aString, and then Haskell will perform (usually) less efficient comparisons.You can however use a language directive:
that you can put at the head of your file. Now this means that if you write a string literal Haskell can interpret this as a
Textelement.So if you write:
Haskell will derive that
ntxthas typeMaybe Text, and thus that"Option 1"should be interpreted as aTextelement.If you use this syntax extension, you can also write:
So you can drop the
fromStringfunction.