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

Class attribute constraints applied on elements of array attribute, not the attribute itself #1414

Open
jkramer opened this issue Jan 16, 2018 · 5 comments

Comments

@jkramer
Copy link
Contributor

jkramer commented Jan 16, 2018

The Problem

When adding a where constraint on a class attribute of type Array (others might be affected as well though), the constraint is applied to the elements of the array instead of the array attribute itself as one would expect.

Expected Behavior

The constraint checks should be performed on the attribute, not its array elements.

Actual Behavior

It seems like the constraint is applied to the elements of the array attribute.

Steps to Reproduce

# This crashes:
class Foo {
  has @.three-things where *.elems == 3;
}.new(three-things => <three excellent things>);

# This works, as apparently the where is applied to each of the three strings
# instead of @.three-things itself:
class Bar {
  has @.three-things where *.chars > 3;
}.new(three-things => <three excellent things>);

# This however works as expected:
sub foo(@three-things where *.elems == 3) {
  say "Got three things! {@three-things.gist}";
}(<exactly three things>);

Environment

@lizmat
Copy link
Contributor

lizmat commented Jan 16, 2018 via email

@zoffixznet
Copy link
Contributor

The where clause is applied to the descriptor of the array, which governs the elements of the array.

How come it applies to the Array itself when it's part of the routine's parameters?

@lizmat
Copy link
Contributor

lizmat commented Jan 16, 2018 via email

@zoffixznet
Copy link
Contributor

zoffixznet commented Jan 16, 2018

https://irclog.perlgeek.de/perl6/2018-01-16#i_15697385

<TimToady> someone probably just implemented the one as an anonymous subset,
       so it distributes like a subset type
<TimToady> we should probably make them consistent 

(there's more discussion in the chat)

P.S.: &-sigils are similarly affected

P.P.S.: there appear to be some glitches with Subset @foo parameter constraints; see here and here

@raiph
Copy link
Contributor

raiph commented Sep 24, 2020

I'm thinking this issue's title should be changed to "Problems with subset/where constraints applied to variables/parameters/attributes with anything other than $ sigil". Any objections?

As background info, I found this bug to be the nearest match for a scenario I just encountered:

my &foo where { True } = sub {}

yields

Type check failed in assignment to &foo; expected Callable[<anon>] but got Sub

In a comment above @zoffixznet has provided links to some IRC chat exchanges and added "P.S.: &-sigils are similarly affected". That reinforces my sense that all such problems belong in one issue until/unless it becomes clear a particular variation needs to be broken out into a separate one.

The IRC links in Zoffix's comment are broken; here's what I think they are supposed to be pointed to:

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

5 participants