@DiazCarrete@hachyderm.io avatar

DiazCarrete

@DiazCarrete@hachyderm.io

This profile is from a federated server and may be incomplete. Browse more on the original instance.

b0rk, (edited ) to random
@b0rk@jvns.ca avatar

poll: to switch branches in git, do you use git checkout or git switch?

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

@b0rk switch all the way, I find checkout confusing because it does way too many things.

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

friendship ended with ScopedTypeVariables, TypeAbstractions is my new best friend
https://serokell.io/blog/ghc-dependent-types-in-haskell-3

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

@BoydStephenSmithJr How do you un-pun your newtypes? Do you use some kind of "Mk-" prefix?

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

In Servant, the ServerError type has an Exception instance
https://hackage.haskell.org/package/servant-server-0.20/docs/Servant-Server.html#t:ServerError
You might speculate that when throwing a ServerError using liftIO . throwIO in a Handler, the ServerError is automatically caught and served as a response, but it ain't so: it's treated as just another exception, and the response code is 500.

Instead, you should throw ServerErrors using "throwError", re-exported from the "Servant" module.
https://hackage.haskell.org/package/servant-server-0.20/docs/Servant.html#v:throwError

#haskell

image/png

DiazCarrete, (edited )
@DiazCarrete@hachyderm.io avatar

@BoydStephenSmithJr I don't think it's a bad idea!

I believe we can impement in Servant the throwIO behavior I mentioned earlier by catching (some) runtime exceptions in the callback passed to "hoistServer" and re-throwing them in the "proper" way expected by Handler. https://hackage.haskell.org/package/servant-server-0.20/docs/Servant-Server.html#v:hoistServer

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

btw, why am I throwing a ServerError with HTTP code 200?

I think ServerError is somewhat misnamed: in reality, it can represent any type of response, not only errors.

It's a kind of escape hatch from the typing strictures of Servant: it lets you return any status code and respose body for a request, not just the ones specified in the type-level API.

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar
DiazCarrete,
@DiazCarrete@hachyderm.io avatar
DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

"Nesting APIs and ReaderT environments with Servant"
https://nicolashery.com/nesting-reader-environments-servant/

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

@fubaroque Making videos about topics that one feels are underexplained is good.

That said, the reference documentarion for the Spring IOC container is already good https://docs.spring.io/spring-framework/reference/core/beans.html but it's a reference, not focused in the big ideas and motivation behind dependency injection / AOP.

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

"Spring Boot - The Missing Guide : 6 - How Spring handles Transactions & Security using AOP/Proxies"
https://www.youtube.com/watch?v=jviq49ukATo

The whole playlist: https://www.youtube.com/playlist?list=PLuNxlOYbv61jZL1IiciTgWezZoqEp4WXh

tweet collecting interesting Spring videos: https://twitter.com/jyotirmaya_das/status/1781887932812698058

Demystifying Spring Internals
https://www.youtube.com/watch?v=LeoCh7VK9cg

#Java #SpringBoot

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

AFAIK, there's not an easy way in Haskell to inspect at the type level what type a field has in a record.

What I mean is that that there doesn't seem to be a type family like

type FieldType :: Type -> Symbol -> Type

that we could invoke in ghci like

:kind! FieldType Person "age"

Why would I want this? For libraries like servant and rel8 that use parameterized records where the types of the fields vary heavily with the type parameter.

I guess I could hack it using generics. 🤔

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar
DiazCarrete,
@DiazCarrete@hachyderm.io avatar

"BLESSED ARE THE POOR IN SPIRIT—Confession of a repentant Effects programmer"

https://abailly.github.io/slides/heureux-les-simples.html

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

I have wondered about this myself:

(24:30) https://youtu.be/fFCcvsbCrH8?t=1468

"registry"-like approaches do seem to make it easier to reuse previously assembled application contexts because the wiring is done through a dictionary-like data structure. You can easily overwrite any key with a different implementation, or with a mock.

Relatedly, they also seem to make it easier to draw diagrams of component dependencies:
https://hackage.haskell.org/package/registry-0.6.1.0/docs/Data-Registry-Dot.html
https://github.com/danidiaz/cauldron/blob/700dafb64ed932eee8b66356e35196c586d7e24c/lib/Cauldron.hs#L123

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

When building #haskell Servant applications with persistence, a common (?) pattern is to request a database connection from a pool in the callback we pass to "hoistServer" https://hackage.haskell.org/package/servant-server-0.20/docs/Servant-Server.html#v:hoistServer and then pass the connection down to the handler using ReaderT https://github.com/danidiaz/comments-project/blob/3bb720124b31f0a8e351751bdcc6651ed75d9e27/comments/lib/Comments/Runner.hs#L57
It works, but I'm kinda unhappy about it because it forces you to use ReaderT in those intermediate components that lie between the top-level handler and the repository component which actually uses the connection. 😕

DiazCarrete,
@DiazCarrete@hachyderm.io avatar
DiazCarrete,
@DiazCarrete@hachyderm.io avatar

Eric Torreborre mentions the need for some form of ReaderT at 33:05 in his "Wire all the things" talk:
https://youtu.be/fFCcvsbCrH8?t=1986

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

As an alternative to ReaderT and/or polymorphism over the effect monad, I could try to implement some form of "thread-local" storage. 🤔 Some kind of map indexed by ThreadId which would be injected only into those components (like the repository) that require the request-scoped info.

Entries would be allocated / deallocated in the "hoistServer" callback, much like with the ReaderT solution.

Intermediate components could use IO for effects and remain blissfully innocent of the shenanigans.

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

The problem of course is that this solution would be less type-safe. If the repository expects an entry for the current ThreadId to exist in the map, and we forgot to set it earlier in the "hoistServer" callback, that would be a runtime error.

It would probably be less efficient as well.

DiazCarrete,
@DiazCarrete@hachyderm.io avatar

You might say "Well, that's not a problem, just make those intermediate components that don't care about the connection polymorphic over MonadUnliftIO! Later, when composing the application, they will be injected a repository that does know about the ReaderT and the connection. Problem solved."

That's true, but what if I wanted those intermediate components to be as dumb as possible? Just plain records of functions in IO, no monad transformers, and no polymorphism over the monad, either?

DiazCarrete, to random
@DiazCarrete@hachyderm.io avatar

In-depth exegesis of the meaning of single letters in my aliases. I'm being so productive you wouldn't believe. Productivity is off the charts right now.

https://github.com/danidiaz/miscellany/blob/4bf8a325165d0e8943fb5a20498c57b0ea8e5a02/linux/.gitconfig#L15

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

TIL that operations on IORefs (unlike some operations on MVars) are are guaranteed not to be interruptible.

https://hackage.haskell.org/package/base-4.19.1.0/docs/Control-Exception.html#g:13

DiazCarrete, (edited ) to haskell
@DiazCarrete@hachyderm.io avatar

https://hackage.haskell.org/package/network-uri-2.6.4.2/docs/Network-URI.html#g:2
https://www.ietf.org/rfc/rfc2396.txt
https://webmasters.stackexchange.com/a/56844/89929

"A relative reference that begins with a single slash character is termed an absolute-path reference. A relative reference that does not begin with a slash character is termed a relative-path reference."

#haskell #rfc

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar
DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

I've published "dani-servant-lucid2" on Hackage. It's a tiny package that provides a HTML content type for Servant, backed by the "lucid2" HTML library.
https://hackage.haskell.org/package/dani-servant-lucid2
https://github.com/danidiaz/dani-servant-lucid2

There was already an integration for an earlier version of lucid https://hackage.haskell.org/package/servant-lucid but not for lucid2. https://github.com/haskell-servant/servant-lucid/issues/26

Also, "dani-servant-lucid2" has a public sublibrary with extra definitions, but it seems as if Hackage doesn't display info for public sublibraries yet.

#Haskell

DiazCarrete, to haskell
@DiazCarrete@hachyderm.io avatar

Rel8 is a #Haskell library that teeters between "looking at the instances provides much useful information" and "looking at the instances causes psychic damage".
https://hackage.haskell.org/package/rel8-1.5.0.0/docs/Rel8.html#t:Table

DiazCarrete,
@DiazCarrete@hachyderm.io avatar
DiazCarrete,
@DiazCarrete@hachyderm.io avatar

Relatedly, it would be a nice feature for Haddock if the instances local to a module could be sorted and grouped for the purposes of documentation and telling a story.

Like "These are the instances for tuples of various sizes" or "this is the base case and these are some instances that recurse".

  • All
  • Subscribed
  • Moderated
  • Favorites
  • JUstTest
  • kavyap
  • DreamBathrooms
  • cubers
  • osvaldo12
  • mdbf
  • magazineikmin
  • normalnudes
  • InstantRegret
  • rosin
  • Youngstown
  • slotface
  • khanakhh
  • ethstaker
  • Leos
  • ngwrru68w68
  • everett
  • cisconetworking
  • tacticalgear
  • anitta
  • thenastyranch
  • Durango
  • tester
  • GTA5RPClips
  • modclub
  • megavids
  • provamag3
  • lostlight
  • All magazines