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

Sum types with aliases #33

Open
dfrese opened this issue Jul 29, 2020 · 5 comments
Open

Sum types with aliases #33

dfrese opened this issue Jul 29, 2020 · 5 comments
Assignees

Comments

@dfrese
Copy link
Member

dfrese commented Jul 29, 2020

Defining sum types does not work when aliasing types or predicates in match expressions, in ClojureScript at least:

(require '[active.clojure.sum-type :as st :include-macros true])
(require '[active.clojure.cljs.record :as rec :include-macros true])

(rec/define-record-type T {:rtd-record? true} (make-t) t? [])
(st/define-sum-type S1 s? [T])
(def S2 S1)
(st/define-sum-type S2 s? [T])
(def T2 T)
(st/define-sum-type S2 s? [T2])

Compile Exception: rtd-record or sum-type required, found: T2

A similar error is raised when using an alias of t? in a match expression.

This is quite a burden for code reuse/abstraction, and should be fixed if possible.

@smoes
Copy link
Contributor

smoes commented Jul 30, 2020

I'm not sure if this is possible. These checks are done at macro-expansion time, how would we handle

(def pred true)
(def T2 (if pred T0 T1))

Various languages offer a function for type-aliasing. Maybe we'd need something like:

(st/defalias T2 T)

Could you provide an example/a rationale why this currently is a burden for code reuse/abstraction?

@dfrese
Copy link
Member Author

dfrese commented Jul 31, 2020

Your "ELM-style" architecture in the Siemens-Client requires a page package to provide a State type, and I wanted to define two almost identical pages by specializing an "abstract page" - delegating what the State type actually is to that package.

Can be worked around to some extent by defining both concrete pages in the same namespace.

@dfrese
Copy link
Member Author

dfrese commented Jul 31, 2020

More detailed example:

(ns utility)
(rec/define-record-type T {:rtd-record? true} (make-t) t? [])

(ns page-1
  (:require [utility :as utility)
(def T utility/T)

(ns home-page
  (:require [page-1 :as page-1]))

(st/define-sum-type State s? [page-1/T])

I wanted to keep the use of the 'utility' module an implementation detail of module 'page-1'.

@smoes
Copy link
Contributor

smoes commented Aug 6, 2020

With this example, I can see why it would be useful. However, we have to introduce a specific macro for that, this cannot be achieved by def afaik.

@smoes smoes removed the bug label Aug 6, 2020
@smoes
Copy link
Contributor

smoes commented Aug 6, 2020

We also have to implement that type-alias operation for records.

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

No branches or pull requests

2 participants