question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disjoint constraints for type parameters

See original GitHub issue

I propose adding a new constraint T1, T2, …, Tn disjoint for signifying type parameters T1, T2, …, Tn are provably disjoint at compile-time with exactly the same algorithm as used for switch cases.

It should enable us to write e. g.

function sumMap<Value1, Value2, Return1, Return2>(Value1|Value2 val, Return1(Value1) f1, Return2(Value2) f2)
    given Value1, Value2 disjoint =>
  switch (val)
  case (is Value1) f1(val)
  case (is Value2) f2(val);

or maybe more useful

Return|Error map…<Value, Error, Return>(Value|Error val, Return(Value) f)
    given Value, Error disjoint =>
  switch (val)
  case (is Value) f(val)
  case (is Error) val;

without disabling any warnings.

Maybe examples aren’t comprehensive enough, but it occurred to me this could be a nice feature and maybe even not hard to implement.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ePaulcommented, Sep 23, 2017

What about given T1 & T2 is Nothing?

Actually, we could also write it the other way around, and reuse the existing syntax: given Nothing satisfies T1 & T2.

This would allow for a future generalization like given String satisfies T1 & T2.

1reaction
gavinkingcommented, Sep 17, 2017

i.e.

Return|Error map<Value, Error, Return>
   (Value|Error val, Return(Value) fun)
        given Value satisfies ~Error 
        => switch (val)
        case (is Value) fun(val)
        case (is Error) val;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 7. Enhanced Entity-Relationship Modelling
There are three constraints that may apply to a specialization/generalization: membership constraints, disjoint constraints and completeness constraints.
Read more >
Supertype/Subtype Disjointness Constraints - YouTube
Chapters. View all · Disjoint Rule Disjoint Rule Definition · Disjoint Rule Disjoint Rule Definition · Disjoint Rule Disjoint Rule Definition.
Read more >
Enforcing Disjoint Unions with TypeScript Conditional Types
One of the more useful ones I've found is a “disjoint” type, which ensures that there are no elements in common between two...
Read more >
disjoint and overlapping design constraints - Stack Overflow
The disjoint rule states an entity instance of a supertype can only be a member of one subtype. The overlap rule ...
Read more >
Disjoint subtyping in SQL
Subtyping is disjoint if an instance of a type corresponds to at most one instance of a subtype. For example, we may have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found