zacwood9 / zacwood.me-comments

Comments on zacwood.me -- provided by utterances
1 stars 0 forks source link

posts/haskell-type-application/ #2

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Haskell's @ symbol - Type Application | Zac Wood

Demystifying a powerful language extension that powers the IHP framework

https://zacwood.me/posts/haskell-type-application/

AbdallahZ7 commented 3 years ago

Thanks for the article, @ always confused me. two questions if I may, what is the hash sign in orderBy #createdAt, I have not seen this before also what is the tilde in table ~ GetTableName model

zacwood9 commented 3 years ago

@abdallahz3 good questions! Those are two more language extensions :) these are a bit more complicated and I don't fully understand them, but here's my understanding of the high level idea of what's going on.

The hash sign comes from the Overloaded labels extension. #createdAt is just syntactic sugar for fromLabel @"createdAt". Our User type would have a instance of HasLabel so that given the label "createdAt", it will return the createdAt field . a bit over my head 😅 but that's the general idea.

The tilde is "type equality". So when we call query @User, it will be filled in as table ~ GetTableName User. Elsewhere in the code we have something like

type instance GetTableName User = "users"

so in the end table turns out to be the type level string "users". This is then used in QueryBuilder to construct the queries.

AbdallahZ7 commented 3 years ago

@zacwood9 That was really helpful, Thanks a lot