kcl-lang / kcl-lang.io

KCL Website and Documentation Repo: https://kcl-lang.io
https://kcl-lang.github.io
Apache License 2.0
12 stars 33 forks source link

[FAQ] Use of mixed-in attributes outside of mixin requires casting to any #353

Closed dennybaa closed 3 months ago

dennybaa commented 3 months ago

Hello @Peefy! Hope I'm not overproducing entropy) The following looks like a bug, I haven't seen it mentioned anywhere.

Steps to reproduce:

schema FooBar:
    mixin [
        FooBarMixin
    ]
    foo: str = 'foo'
    bar: str = 'bar'

protocol FooBarProtocol:
    foo: str
    bar: str

mixin FooBarMixin for FooBarProtocol:
    foobar = "${foo}.${bar}"

_c = FooBar {}
foobar = _c.foobar

Expected output:

foobar: foo.bar

Obviously casting to any mitigates the issue _c = {**FooBar{}}. Thanks a lot :+1: !

Peefy commented 3 months ago

Do you mean this

schema FooBar:
    mixin [
        FooBarMixin
    ]
    foo: str = 'foo'
    bar: str = 'bar'

protocol FooBarProtocol:
    foo: str
    bar: str

mixin FooBarMixin for FooBarProtocol:
    foobar: str = "${foo}.${bar}" # Attribute with the annotation can be accessed outside the schema.

_c = FooBar {}
foobar = _c.foobar
dennybaa commented 3 months ago

Yep _c.foobar, this one, just the example above, produces the following error:

Error: failed to compile the kcl package

error[E2G22]: TypeError
  --> ...a.k:17:10
   |
17 | foobar = _c.foobar
   |          ^ attribute 'foobar' not found in 'FooBar', did you mean '["foo"]'?   ```
dennybaa commented 3 months ago

Ah sorry, it might be I need to add specifically into the schema...( Sorry! This is a bit confusing, but it's okay.

d4v1d03 commented 3 months ago

@Peefy , made the PR #381