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

APIRequest型クラス #10

Open
Cj-bc opened this issue Feb 19, 2020 · 5 comments
Open

APIRequest型クラス #10

Cj-bc opened this issue Feb 19, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@Cj-bc
Copy link
Owner

Cj-bc commented Feb 19, 2020

概要

型クラスを使う事で、明示的に各API用のRequestを作らなくてよくなる(伝われ)

実装例

class APIRequest a where
    defaultRequest :: a
    -- Create APIRequest with minimum requirement
    fromMinimum :: String -> a

メリット

  • いちいち各APIのモジュールをqualified importしてUN.APIRequest ...としなくて済むようになる

課題

  • fromMinimumの型が決まらない
  • fromUserIDなどとしたいが、 userIDを使用しないAPIもあるので悩ましい
@Cj-bc Cj-bc added the enhancement New feature or request label Feb 19, 2020
@Cj-bc
Copy link
Owner Author

Cj-bc commented Feb 22, 2020

class APIRequest a => FromUserId a where
  fromUserId :: UserId -> a

class APIRequest a => FromNoteId a where
  fromUserId :: NoteId -> a

みたいに作る??

@Cj-bc
Copy link
Owner Author

Cj-bc commented Feb 16, 2021

思ったんですが、なんか「aがこの型の場合はbをこの型に固定する」みたいな仕様ありませんでしたっけ。
a | a -> bみたいな(覚えてないので調べること)

もしあるとすれば、APIRequest型クラスの型引数を増やして(ghc拡張にはなる)対応できるかも。
でも果たしてそれが良い手なのかはわからない

@Cj-bc
Copy link
Owner Author

Cj-bc commented Feb 16, 2021

Data families若しくはtype familyでできそう

https://qiita.com/lotz/items/6c038698c8f04f57113a

@Cj-bc
Copy link
Owner Author

Cj-bc commented Feb 16, 2021

class APIRequest a where
    _id :: Text -> Maybe a
    _id = Nothing

    sinceId :: Id -> Maybe a
    sinceId _ = Nothing

    untilId :: Id -> Maybe a
    untilId _ = Nothing

    ...

などと全ての「引数にとりうるもの」からAPIRequestを実装している型を作り出すクラスにすると楽かも?

@Cj-bc
Copy link
Owner Author

Cj-bc commented Feb 18, 2021

Lensでよくないですか??????

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

No branches or pull requests

1 participant