Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Getting a variables type as a string at runtime (Gareth Morgan)
2. Re: Getting a variables type as a string at runtime (fa-ml)
3. Re: Getting a variables type as a string at runtime
(Gareth Morgan)
4. Re: Getting a variables type as a string at runtime
(Brandon Allbery)
5. Re: Getting a variables type as a string at runtime
(Gareth Morgan)
6. Re: Getting a variables type as a string at runtime
(Benjamin Edwards)
----------------------------------------------------------------------
Message: 1
Date: Sun, 12 Jan 2014 14:04:40 +0000
From: Gareth Morgan <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Getting a variables type as a string at
runtime
Message-ID:
<CAN3BLMgHwAv+KoHDj3ByvFhNQJD3HbLPGpLEAEXcJdQFNt_=a...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Bit of a simple question but I'm struggling to find information on this via
Google.
I have a sum type, when the input is type A I want to process it, when it
is type B I want to return MyError "Expected type A but actually got type
B". However I also have C,D,E,F, etc which should also return similar
errors so don't want to hardcode this string.
Is there a function I can call on a variable to get the name of the data
constructor that created it? I could create a typeName function but I want
to avoid boiler plate if I can.
For reference what I'm actually doing is parsing Java class files. The
constant pool I've represented as a map of a sum ConstantPoolEntry type
(the Java constant pool for some reason skips an index on some types so
can't be a simple list). Certain Java class file entries have a reference
to the constant pool that must be of a specific constant type so I want to
report badly formed entries to the user.
Thanks,
Gareth
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140112/17b51ae4/attachment-0001.html>
------------------------------
Message: 2
Date: Sun, 12 Jan 2014 15:33:57 +0100
From: fa-ml <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Getting a variables type as a string
at runtime
Message-ID: <20140112143357.GB23813@efikamx>
Content-Type: text/plain; charset="us-ascii"
On Sun, Jan 12, 2014 at 02:04:40PM +0000, Gareth Morgan wrote:
> I have a sum type, when the input is type A I want to process it, when it
> is type B I want to return MyError "Expected type A but actually got type
> B". However I also have C,D,E,F, etc which should also return similar
> errors so don't want to hardcode this string.
Would pattern matching be enough for you? i.e. (very crude):
data Test = Alfa | Beta | Gamma deriving (Show)
testAlfa Alfa = undefined -- put your process function here
testAlfa o = error $ "wasn't expecting " ++ show o
main = testAlfa Beta
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140112/37d99304/attachment-0001.sig>
------------------------------
Message: 3
Date: Sun, 12 Jan 2014 14:47:20 +0000
From: Gareth Morgan <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Getting a variables type as a string
at runtime
Message-ID:
<can3blmga_mkzsg8ucaujuxmdmh7yf02h6w+-2u5dynafz79...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Won't show include all the components? I wanted to include only the type
name.
I could probably get away with bundling all the contents into the error
string but it would be nice to know if there is a standard way to get type
names like this.
On Sun, Jan 12, 2014 at 2:33 PM, fa-ml <[email protected]> wrote:
> On Sun, Jan 12, 2014 at 02:04:40PM +0000, Gareth Morgan wrote:
> > I have a sum type, when the input is type A I want to process it, when it
> > is type B I want to return MyError "Expected type A but actually got type
> > B". However I also have C,D,E,F, etc which should also return similar
> > errors so don't want to hardcode this string.
>
> Would pattern matching be enough for you? i.e. (very crude):
>
> data Test = Alfa | Beta | Gamma deriving (Show)
>
> testAlfa Alfa = undefined -- put your process function here
> testAlfa o = error $ "wasn't expecting " ++ show o
>
> main = testAlfa Beta
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140112/89434c11/attachment-0001.html>
------------------------------
Message: 4
Date: Sun, 12 Jan 2014 09:53:03 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Getting a variables type as a string
at runtime
Message-ID:
<cakfcl4vg-fsbq9-mqns+r10_aix4o6jpqx3lvbsfut_gyek...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan <[email protected]>wrote:
> Won't show include all the components? I wanted to include only the type
> name.
>
Types only exist at compile time normally. But if you add a Typeable
constraint (see Data.Typeable and the DeriveDataTypeable extension) you can
get type names at runtime.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140112/fefc0fbf/attachment-0001.html>
------------------------------
Message: 5
Date: Sun, 12 Jan 2014 15:45:17 +0000
From: Gareth Morgan <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Getting a variables type as a string
at runtime
Message-ID:
<CAN3BLMij+-=qyrar6smxtnfvk06yf_imszul7lrkmkstu0b...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Data.Typeable with Data.Data worked. Thank you.
Using Typeable alone was only giving me the type of the whole type.
Data.Data had the toConstr function needed to give me the particular
constructor name.
For reference if anyone is interested in the solution:
import Data.Typeable
import Data.Data
data A = A | B | C deriving (Typeable, Data)
typeName :: A -> String
typeName = show . toConstr
I needed to add DeriveDataTypeable extension to my cabal file
On Sun, Jan 12, 2014 at 2:53 PM, Brandon Allbery <[email protected]>wrote:
> On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan <[email protected]>wrote:
>
>> Won't show include all the components? I wanted to include only the type
>> name.
>>
>
> Types only exist at compile time normally. But if you add a Typeable
> constraint (see Data.Typeable and the DeriveDataTypeable extension) you can
> get type names at runtime.
>
> --
> brandon s allbery kf8nh sine nomine
> associates
> [email protected]
> [email protected]
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140112/e8db0d99/attachment-0001.html>
------------------------------
Message: 6
Date: Sun, 12 Jan 2014 21:22:55 +0000
From: Benjamin Edwards <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Getting a variables type as a string
at runtime
Message-ID: <-2953521251767591960@gmail297201516>
Content-Type: text/plain; charset="iso-8859-1"
If you just wanted the constructor name, show would have worked perfectly.
On Sun Jan 12 2014 at 15:45:28, Gareth Morgan <[email protected]> wrote:
> Data.Typeable with Data.Data worked. Thank you.
>
> Using Typeable alone was only giving me the type of the whole type.
> Data.Data had the toConstr function needed to give me the particular
> constructor name.
>
> For reference if anyone is interested in the solution:
>
> import Data.Typeable
> import Data.Data
>
> data A = A | B | C deriving (Typeable, Data)
>
> typeName :: A -> String
> typeName = show . toConstr
>
>
> I needed to add DeriveDataTypeable extension to my cabal file
>
>
> On Sun, Jan 12, 2014 at 2:53 PM, Brandon Allbery <[email protected]>wrote:
>
> On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan <[email protected]>wrote:
>
> Won't show include all the components? I wanted to include only the type
> name.
>
>
> Types only exist at compile time normally. But if you add a Typeable
> constraint (see Data.Typeable and the DeriveDataTypeable extension) you can
> get type names at runtime.
>
> --
> brandon s allbery kf8nh sine nomine
> associates
> [email protected]
> [email protected]
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140112/43d31e31/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 67, Issue 16
*****************************************