You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

1.9 KiB

I'm confused with class get set and prop

We have 2 places in documentation describing it:

[A] https://imba.io/docs/basic-syntax/classes#computed-properties

[B] https://imba.io/docs/basic-syntax/operators#keywords-set

[A] describes:

get

The get syntax binds an object property to a function that will be called when that property is looked up

But there's no bind. If you call get the same as property you get into infinite loop. If you call it differently - like in examples on website which doesn't support the description clame - then it works like a regular function and no difference between using get or def other than self documenting syntax.

1. What's the differenece between get and def?

set

Using set you can define a function that will be called when there is an attempt to set that property.

Sounds clear but then example doesn't show it and then it's not clear until you check it. Proper example:

class Rect
    sides = 0
    width = 0
    height = 0

    set sides size
        width = size
        height = size

I would expect different behaviour from set - method is called on value change > process value > returns processed value > applies returned value instead. Probably the thing which is proposed in undocumented section as $set but with no practical syntax.

2. I see no consistency between get and set as set binds on write and get doesn't bind on lookup.

No prop description or usage in any example of Class documentation.

[B] describes prop and use it in examples:

prop

A prop allows a tag property to be set from the outside

But you have the same behaviour without keyword prop.

3. What's the difference between following?

class A
    prop a = 1
        
# and

class B
    b = 1