Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's another attempt:</p> <pre><code>class =!=[A, B] private () extends NotNull object =!= { implicit def notMeantToBeCalled1[A, B &gt;: A, C &gt;: B &lt;: A]: =!=[B, A] = error("should not be called") implicit def notMeantToBeCalled2[A, B &gt;: A, C &gt;: B &lt;: A]: =!=[B, A] = error("should not be called") implicit def unambigouslyDifferent[A, B](implicit same: A =:= B = null): =!=[A, B] = if (same != null) error("should not be called explicitly with the same type") else new =!= } case class Foo[A, B](a: A, b: B)(implicit ev: A =!= B) </code></pre> <p>Then, again:</p> <pre><code>// compiles: Foo(1f, 1.0) Foo("", 1.0) Foo("", 1) Foo("Fish", Some("Fish")) // doesn't compile // Foo(1f, 1f) // Foo("", "") </code></pre> <p>Like in my other proposal, the aim here is to introduce a compile-time ambiguity when <code>A</code> and <code>B</code> are the same. Here, we provide two implicits for the case where <code>A</code> is the same as <code>B</code>, and an unambiguous implicit when this is not the case.</p> <p>Note that the problem is that you could still explicitly provide the implicit parameter by manually calling <code>=!=.notMeantToBeCalled1</code> or <code>=!=.unambigouslyDifferent</code>. I couldn't think of a way to prevent this at compile time. However, we can throw an exception at runtime, with the trick that <code>unambigouslyDifferent</code> requires an evidence parameter itself indicating whether <code>A</code> is the same as <code>B</code>. But wait... Aren't we trying to prove the exact opposite? Yes, and that's why that <code>same</code> implicit parameter has a default value of <code>null</code>. And we expect it to be <code>null</code> for all legal uses — the only time where it would not be <code>null</code> is when a nasty user calls e.g. <code>Foo(1f, 1f)(=:=.unambiguouslyDifferent[Float, Float])</code>, and there we can prevent this cheating by throwing an exception.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload