Hi Vadim, This is for you and anyone else interested in a little bit about tricky aspects of sub-types, of P6, of my perspective on learning in response to WTF? moments, and P6 strengths and weaknesses in relation to the foregoing.
Vadim wrote: > my Array[Str] $a = ["a", "b", "c"]; > my Str @a = ["a", "b", "c"]; > > look at the situation from the decent user (which is what I am most of the time) perspective. He takes the second line, sees that it works as excepted. Then he takes what @a.WHAT reports back and sees that it is actually Array[Str]. He tries the first line and... boomer? "Type check failed in assignment to $a; expected Array[Str] but got Array"!! What??? Well first, to speak in generalities, the user would get a WTF? hit. Aiui, in brain science terms it's https://en.wikipedia.org/wiki/Error-related_negativity The underlying brain science shows that if you can figure out what's really going on then this error related negativity is actually a very positive thing -- a great way to learn, perhaps the best there is. As such it feels bad but having these moments, if we learn appropriately from them, is actually *better* than not having them, if we want to learn. To quote http://changingminds.org/explanations/learning/surprise_learning.htm > So what happens after surprise? It is not always learning. For example, we might ignore the event as beneath interest or explain it away as an anomaly. > But if it is important and if it happens again, we have to change our method prediction, and the critical thing we often change is the mental models we use to understand and explain the world. This is learning. Here's an older Scientific American article about the same thing. https://www.scientificamerican.com/article/learning-by-surprise/ (NB. I haven't read either of these articles I've linked other than to confirm they look reasonable. I just googled ""surprise and learning" because I know the related brain science stuff and knew something would turn up.) But let's move on from these generalities. In this case there were at least two surprises for you related to the example you gave, not just one. First, there's co/contra variance of sub-typing. The first paragraph of wikipedia page is https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science) introduces this issue well imo. There's no way to avoid this issue it if you want to use a language that supports both sub-types (like a Cat is an Animal) and lists. In regard to co/contra variance Perl 6 actually delays the tougher WTFs that will come with time. You have more WTFs to look forward to. :) Second, there's list vs scalar assignment. This is a consequence of distinguishing single things from collections of things. In contrast to co/contra variance, Perls force this issue to be confronted early on. But this is a good thing. There's no way to avoid it if you want to use a language that distinguishes between single things and collections and this distinction is more central to the design of Perls than it is for many other languages. So P6 brings this surprise to you early on so you can also learn the power this approach brings early on. But yes, what you did brought on double the dose of ERN, so double the ouch, and at least two things to learn simultaneously to explain the behavior, not one. The pain isn't worth it if a user doesn't update their mental model in response to the pain. Which may happen if the user's response to the surprise is to be overwhelmed. But if they learn, then it's worth it. They don't have to necessarily learn or retain all there is to learn immediately. In fact that's essentially impossible; that's not how our brains work. But after a few repeats. So the issue arguably boils down to how good are the learning materials and processes. As you've pointed out, the docs are weak in places. Arguably in far too many places. Another very important aspect is error messages. P6 aims at having awesome error messages. But that can get difficult too. In summary, in response to WTFs in general, I urge patience. And for these particular ones, as with most in P6, the underlying concepts aren't impossibly hard to grasp for mere mortals but sometimes our learning materials aren't good enough. Please consider filing bug reports about this if you have time and still feel the teachability value of the WTF? could be better handled. > because the = is doing list assignment, i.e. assigning each of the elements on the right into a Str type constrained Scalar on the left and all of those assignments pass the individual scalar type checks. > > > But this difference between list and scalar assignment isn't mentioned anywhere on docs.perl6.org. All one can find is that they exists and have different precedence. I see discussion of the difference there, but clearly it's not sufficiently discoverable and/or clearly explained. Imo the docs continue to be a weak spot and continue to improve. Please consider searching for and/or filing a bug at https://github.com/perl6/doc/issues > Otherwise the difference between the two assignments is quite counterintuitive and, to my view, it contradicts to DWIM. "intuitive" is a subjective concept. Intuition is based on what one knows. I know of several differences; I find them all intuitive. (This is perhaps why I was able to write about some of them and your response was that what I wrote made sense.) So they do what *I* mean. But of course you're you not me... One apparently good strategy for increasing the likelihood someone sees something as intuitive is to assume they know as little as is it is reasonable for them to know. Ideally, one assumes nothing. To a degree P6 attempts that. But there are limits to that strategy. For example, the reality is that you know *P5*. And it's likely that part of the reason things feel counter-intuitive is that they *are* counter-intuitive for you, because you know P5. I don't know if that's relevant in this instance because I don't know what specific aspect of what happens on assignment you're finding counter-intuitive. > Similar case is with Array ~~ List against List ~~ Array. If for any reason one needs to test if a value could be assigned to a container prior to the actual assignment it would really be hard to grasp for the first time why my @a = <a b c> works whereas my Array $a = <a b c> fails. I think your confusion here is to do with (our materials and error messages related to) container semantics. Does https://opensource.com/article/18/8/containers-perl-6 help? I'm happy to dialog here about any of this stuff help clear up your mental models (and mine). -- raiph
