Aug. 28th, 2014
handling a pointer in Scala code
Aug. 28th, 2014 03:40 pmA pointer can be an ID of something (people like ids), or a Url, whatever.
Approach 1. Native type.
Approach 2. Specialized type.
This week I had made two errors in my code; in one I had mixed
Approach 3. Name variables right. If it is an identifier of a claim pdf, call it
So, approach 4. Phantom types they are called, are not they?
Introduce parameters in these types:
Other solutions?
Approach 1. Native type.
String
for url, Long
for id. So, you become just a number; your id can be multiplied by 0 or by 7; and a reference to a page has method like substring()
or replaceAll()
, or +
. We can add a url and user id, and it will be okay in this world. Typeless!Approach 2. Specialized type.
case class ID(private val value: Long)
, class Url(private val text: String)
; this makes our code more sane. We only compare urls with urls and ids with ids; and we can stash them in sets or maps, using as keys (ok, need hashCode
and equals
).This week I had made two errors in my code; in one I had mixed
ID
of a company and ID
of a "connector table entry" that would point to a company; in another I had mixed Url
of the main page and Url
of a pdf file; in the latter case I just used an identifier, url
, and it was a wrong identifier.Approach 3. Name variables right. If it is an identifier of a claim pdf, call it
urlOfClaimPdf
. Makes sense; but not always: we can have some method that does not care if it is a corn pdf or a porn pdf. It only needs to be a pdf.So, approach 4. Phantom types they are called, are not they?
Introduce parameters in these types:
IDof[User]
, IDof[Pdf]
, or UrlOf[ClaimPdf]
where ClaimPdf <: Pdf
, and UrlOf[+T]
is covariant. On many occasions it will be just a compiler warning, but well, it's way better to have a warning than to go blind, right?Other solutions?