tagged types

55 views
Skip to first unread message

Matthew Pocock

unread,
Feb 23, 2012, 9:59:36 AM2/23/12
to sca...@googlegroups.com
Hi,

I find the tagged types in scalaz7 very useful. I'm using them to keep data representation and interpretation more separate, which is removing lots of bugs from my code. The @@ syntax for defining types is nice. However, the syntax for tagging values is ugly, and for me makes code significantly harder to read.

val appleCount = Tag[Long, Apple](23L)

Could we re-use the @@ syntax here, and introduce something more like:

val appleCount = 23L.@@[Apple]

Thanks,

Matthew

--
Dr Matthew Pocock
Integrative Bioinformatics Group, School of Computing Science, Newcastle University
skype: matthew.pocock
tel: (0191) 2566550

Jason Zaugg

unread,
Feb 23, 2012, 10:15:42 AM2/23/12
to sca...@googlegroups.com
On Thu, Feb 23, 2012 at 3:59 PM, Matthew Pocock
<turingate...@gmail.com> wrote:
> Hi,
>
> I find the tagged types in scalaz7 very useful. I'm using them to keep data
> representation and interpretation more separate, which is removing lots of
> bugs from my code. The @@ syntax for defining types is nice. However, the
> syntax for tagging values is ugly, and for me makes code significantly
> harder to read.
>
> val appleCount = Tag[Long, Apple](23L)
>
> Could we re-use the @@ syntax here, and introduce something more like:
>
> val appleCount = 23L.@@[Apple]

I settled on:

Apple(23L)

Beware that values of these types can't be passed to varargs methods,
you get a runtime ClassCastException as the compiler thinks we have a
reference type (its not exactly one the AnyVal types).

I think specialization will also fail to work with these types.

I've got a patch to fix the first problem, but I'm not sure it would
be accepted into Scala. Value classes, which might make it into Scala
2.10, will provide an officially sanctioned way to do this without
resorting to, as we essentially do now, 1.asInstanceOf[Int with Tag]

-jason

Matthew Pocock

unread,
Feb 23, 2012, 10:24:50 AM2/23/12
to sca...@googlegroups.com
On 23 February 2012 15:15, Jason Zaugg <jza...@gmail.com> wrote:
On Thu, Feb 23, 2012 at 3:59 PM, Matthew Pocock
<turingate...@gmail.com> wrote:
 
> Could we re-use the @@ syntax here, and introduce something more like:
>
> val appleCount = 23L.@@[Apple]

I settled on:

 Apple(23L)

This doesn't work very well for my kinds of uses. For example, if I have a String that represents a URL, I could use the type String @@ URL. It then becomes rather ambiguous to the coder if URL("http://www.wikipedia.org") has the type String @@ URL or the type URL.

I'm not suggesting this is necessarily a best-practice way to use tagged types, and perhaps I should have some other stuff in there (e.g. String @@ Represents[URL]) but I've really not played with this enough to know how useful it is to heavily encode my domain ontology into the tags like this. Also, I'm not sure how I would support Apple(23L), Orange(12L), Banana(42L) without three lots of (near) identical boilerplate, which rather defeats the attraction of having ad-hoc tagging.

Matthew
 

Beware that values of these types can't be passed to varargs methods,
you get a runtime ClassCastException as the compiler thinks we have a
reference type (its not exactly one the AnyVal types).

I think specialization will also fail to work with these types.

I've got a patch to fix the first problem, but I'm not sure it would
be accepted into Scala. Value classes, which might make it into Scala
2.10, will provide an officially sanctioned way to do this without
resorting to, as we essentially do now, 1.asInstanceOf[Int with Tag]

-jason

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To post to this group, send email to sca...@googlegroups.com.
To unsubscribe from this group, send email to scalaz+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/scalaz?hl=en.

Reply all
Reply to author
Forward
0 new messages