Code Monkey home page Code Monkey logo

Comments (1)

LucySMartin avatar LucySMartin commented on July 3, 2024 1

I don't nessaserilly agree with the assessment that this is removing an additional check - its simply erroring before you get to the check that you have in both.

If we break down the logic a bit more, in the first example this is trying to break down example into a tuple, and then attempting to pattern match inside of it.

In the second example, a and b do not exist unless you have already successfully run extractors on a Tuple2[Int, Int] - which is going to fail.

Imagine a case where the inner extractor was not a simple type check, but passing through to some custom unapply method, which accepted only positive Ints, and ascii strings. In this hypothetical, the user in the first case would be asking "Do I have two fields which are either posative ints or ascii strings" and in the second is asking "Do I have exactly two posative ints".

We could also consider examples which were more complex than a simple tuple. For example class Foo[T](foo: Int | T). In this case an extractor of Foo[Int](a: int) is clearly stronger than Foo(a:Int) - and it would be up to the implementation of Foo.unapply to determine how this further refinement was handled. In the case of case classes (and thus tuples) - unapply promises to preserve known types - and thus has to fail when the types is wrong.

If we wanted to make this work it would be opening up a whole heap of things in constructs like val Tuple2[Int,Int](a, b) = (???,???) with a and b not getting the fully validated type.

from scala3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.