Aug. 28th, 2014

juan_gandhi: (VP)
A pointer can be an ID of something (people like ids), or a Url, whatever.

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?
juan_gandhi: (VP)
In the code I'm working with.

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

June 2025

S M T W T F S
1 2345 6 7
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 8th, 2025 05:17 am
Powered by Dreamwidth Studios