elm-pages icon indicating copy to clipboard operation
elm-pages copied to clipboard

Capture only-internal pages and images in type

Open j-maas opened this issue 4 years ago • 0 comments

(This only makes sense with functions that only work on internal pages and images, such as ImagePath.dimensions from #110.)

I recently came across an article about using phantom types to exclude a constructor from a type. I think this might be useful here to allow pages and images to be restricted to internal, statically checked ones only. Then we could remove the Maybe from e.g. ImagePath.dimensions, if it only accepts ImagePath Key Internal instead of ImagePath Key all.

The idea is to mark internal images (and pages) on the type level with a phantom type, such that they are ImagePath Key Internal, whereas you could also write ImagePath Key all where the type variable would match both the Internal and External.

Then ImagePath.dimensions could take an ImagePath Key Internal and thus no longer needs to care about external images. Then the Maybe would be moved to an extractInternal : ImagePath Key all -> Maybe (ImagePath Key Interal). If you have both internal and external images, this is basically the same as now when you write ImagePath.extractInternal myImage |> Maybe.map ImagePath.dimensions.

But all the images provided by Pages.elm could be ImagePath Key Internal and not require the Maybe, and if you store them somewhere, this method would allow you to declare that you don't accept external images and not deal with the Maybe there either.

I'm not yet sure if this is a worth-while tradeoff, but I wanted to throw the idea out there.

j-maas avatar May 24 '20 08:05 j-maas