That’s about all it shows. The syntax ‘[]=’ to append to an array is typical php WTF-ness!

But I did have a quick look at the parser and, I sort of hate to say it, but it’s actually a pretty simple tweak to allow no args for array indexing. With a 2 line change to parser.monkey2 I can compile this…

Monkey

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

ClassC

Operator[]=(key:String,value:String)

Print"key="+key+", value="+value

End

Operator[]=(value:String)

Print"value="+value

End

End

FunctionMain()

Localc:=NewC

c["one"]="two"

c[]="Hello there!"

End

…which isn’t quite what you wanted, but enough to be able to do what you want I believe?

I still really dislike []= to append to an array though, boring old Method Add() is IMO a much better option!

Sorry, but it still looks kind of nasty to me. And aren’t you gonna confuse a whole bunch of php users?!?

I’m probably not quite as adverse to overloading ‘=’ as I used to be though. I assume the goal here is still to achieve ‘property changed’ events/signals?

I mean, you can already do this quite easily:

Monkey

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

ClassMyLife

FieldLifestyleChanged:Void()

PropertyLifestyle:Int()

Return_lifestyle

Setter(lifestyle:Int)

Iflifestyle=_lifestyle Return

_lifestyle=lifestyle

LifestyleChanged()

End

Private

Field_lifestyle:Int

End

…but of course you know this! If the goal is to wrap both the signal and the setter/getter in a single concept, you can do this too, eg:

Monkey

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

StructMyProperty<T>

FieldChanged:Void()

Method Set(value:T)

Ifvalue=_value Return

_value=value

Changed()

End

Method Get:T()

Return_value

End

Private

Field_value:T

End

ClassMyLife

FieldLifetyle:MyProperty<Int>

End

This is effectively what your (mostly) doing above, except you need to use Lifestyle.Set() and Lifestyle.Get() to set/get the value, plus you can listen for changes using Lifestyle.Changed+=Lamba…

IMO, the []= syntax isn’t enough of an improvement over plain Set and Get here to justify it. In fact I’d argue Get and Set is much clearer than an ‘array looking’ operator. And either way, the programmer has to ‘know’ to use it (if you see what I mean).

Overloading ‘=’ is much more interesting though – it means ‘live’ properties like this could be added to existing code without user code knowing it wasn’t just reading/writing files/properties. But for this work, there’d also have to be a ‘conversion’ operators for reads. Perhaps something like:

Monkey

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

StructMyProperty<T>

FieldChanged:Void()

Operator Setter(value:T)

Ifvalue=_value Return

_value=value

Changed()

End

Operator Getter:T()

Return_value

End

Private

Field_value:T

End

ClassMyLife

FieldLifetyle:MyProperty<Int>

End

But alas we’ve already hit our first complication! If the ‘type’ of Lifestyle is Int thanks to its getter operator, how can we access Lifestyle.Changed?

And if T is a class/struct, what does Lifestyle.blah mean? Is it a member of Lifestyle, or a member of LifeStyle.Getter? The compiler could attempt to guess what you want to do, but it’d likely result in a bunch of confusing rules, and type conversions would be a nightmare when calling functions, assigning to vars etc. And what happens with prop1=prop2? If they are both Int Getters of the same type, should it get/set the int, or assign the entire getter, ‘Changed’ listeners and all? The sensible thing to do in all these cases is probably to treat any value with a Getter as a value of the Getters type instead (just like properties), and add new syntax to somehow ‘ignore’ the getter when you want work with the actual property-like container.

All in all it’s an interesting idea and I think it could be made to work, will keep thinking about it….

If the ‘type’ of Lifestyle is Int thanks to its getter operator, how can we access Lifestyle.Changed?

yes! it’s looks impossible, and I miss it in my minds, if we have getter that return any type – we cant easily access to our parent type,

the way via casting is not what we want.

And what happens with prop1=prop2? If they are both Int Getters of the same type, should it get/set the int, or assign the entire getter, ‘Changed’ listeners and all? The sensible thing to do in all these cases is probably to treat any value with a Getter as a value of the Getters type instead (just like properties), and add new syntax to somehow ‘ignore’ the getter when you want work with the actual property-like container.

I expected to have type conversion for expressions with our type, but not for dot access case, but I see now that it’s kind of impossible. 🙂

Even if we’ll have assignment operator we’ll have no proper get operator and need to write getter explicitly:

Monkey

1

2

3

Localprop:=NewProperty<String>

prop="hello"

Locals:=prop.Value' or prop.Get()

Operator
To:T can help in some cases but not all.

And I think now that using explicit
prop.Value for setter and getter is the best solution.

The general idea of using custom operators is to reduce “verbose” syntax. Instead of having a pumped API that makes you bored to hell to use it. You would cleverly create important domain operations and thus create such shortcuts.

0

Author

Posts

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

Search for:

Archives

Archives

Recent Replies

Warning: Cannot modify header information - headers already sent by (output started at /home/monkey2monkeyx/public_html/wp-includes/class.wp-styles.php:242) in /home/monkey2monkeyx/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220$100 Bounty: MX2 Running on Samsung's Linux on Dex

Warning: Cannot modify header information - headers already sent by (output started at /home/monkey2monkeyx/public_html/wp-includes/class.wp-styles.php:242) in /home/monkey2monkeyx/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220Need Monkey2 logos as .SVG

Warning: Cannot modify header information - headers already sent by (output started at /home/monkey2monkeyx/public_html/wp-includes/class.wp-styles.php:242) in /home/monkey2monkeyx/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220Viva Mortis

Warning: Cannot modify header information - headers already sent by (output started at /home/monkey2monkeyx/public_html/wp-includes/class.wp-styles.php:242) in /home/monkey2monkeyx/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220Viva Mortis

Warning: Cannot modify header information - headers already sent by (output started at /home/monkey2monkeyx/public_html/wp-includes/class.wp-styles.php:242) in /home/monkey2monkeyx/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220Viva Mortis

What is this?

Monkey2 is an easy to use, cross platform, games oriented programming language from Blitz Research.