Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Second type parameter for filename type, with a typeclass to convert to/from FilePath #18

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

jefdaj
Copy link

@jefdaj jefdaj commented Apr 28, 2024

Waaay back in 2018 I lazy-forked your code (pasted it locally) to see if I could fit larger trees in RAM by changing how filenames are encoded. I found that at least for my use case, Text with strictness annotations was better. But I wasn't sure how to contribute that upstream without breaking everyone else's Strings.

I'm finally getting back into Haskell and cleaning up some old code, and had a new idea!

I added a typeclass IsName with the 2-3 functions you would need to supply to work with a new filename type, and a trivial instance for FileName:

class IsName n where
  p2n :: FilePath -> n
  n2p :: n -> FilePath
  nappend :: FilePath -> n -> FilePath
  nappend p n = p </> n2p n

instance IsName FileName where
  p2n = id
  n2p = id

What do you think??

It turns out not to require much beyond adding another type parameter to all the function signatures. Test.hs still passes, and so do all the tests in my code, which uses a newtype wrapper around Text instead. (Confusingly split across two different files here and here)

I left the anchors as FilePath because they can have slashes in them, and because I never had a situation where I needed a lot of them so memory use doesn't seem important.

If you like it I can clean it up a bit more, remove unrelated changes to the nix flake, comments etc. And/or change the names if you have opinions about them. I just used the first ones that came to mind and weren't confusing versions of "filename" or "path".

I haven't looked at the examples yet. And I commented out all the lens code because I don't understand how to update it. I made a quick attempt in a new branch though and might have gotten close?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant