[REQUEST] Add 'With…End With' Statement in Monkey2 language
August 14, 2017 at 11:11 pm #9828
Can you add the statement “With … End With” for the language Monkey2?
It can be a good addition to the language, moreover, it allows to clarify the source code and avoids redundancies. Especially with objects that contains other objects, it can be very long … 🙂
I have no idea about the work that it represents, I let you decide.
Here is an example of an implementation in Visual Basic: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement
Example VB Code :123456789101112Dim theWindow As New EntryWindowWith theWindowWith .InfoLabel.Content = "This is a message.".Foreground = Brushes.DarkSeaGreen.Background = Brushes.LightYellowEnd With.Title = "The Form Title".Show()End With
Translate to Monkey2123456789101112Local theWindow:= New EntryWindowWith theWindowWith .InfoLabel.Content = "This is a message.".Foreground = Brushes.DarkSeaGreen.Background = Brushes.LightYellowEnd With '// 'With' keyword optional for closing block.Title = "The Form Title".Show()End WithAugust 18, 2017 at 12:02 am #9895
Still can’t decide how much/if I like that.
It doesn’t help that the example doesn’t make any sense (where is InfoLabel declared?) but I can’t really see myself using it, I guess if I had to initialize a massive struct or something…
Any other opinions on this? I think Amiga blitz actually had something like this, but it looks like it didn’t have much of an impact on me!August 18, 2017 at 12:47 am #9896
Ahh I see it’s just so you don’t have to reference the class you’re messing with over and over as far as I can tell.123456789101112' so instead oftheWindow.Property = "this"theWindow.Property1 = "that"theWindow.Property2 = "and this"' it would beWith theWindow.Property = "this".Property1 = "that".Property2 = "and this"End
GML from Game Maker as something like this only the syntax is a bit changed, it allows you to edit collections of objects by using “all” or a single parent class.12345678' How it might look in Monkey (Entities would be a collection of normal Entities)With Entities.All().Position = New Vec2f(0.0, 0.0).Material = ... ' Blah blahEnd' Although this could be done with a loop, or by copying an object from the heap
This would be useful for some but I personally don’t really think it would be too necessary.
Very cool concept though!August 18, 2017 at 8:13 am #9899
“With statement” work with ObjectClass expression only (class if you want use static function/variable or instance of class) and use “.” (dot) or “->”, if object’s pointer, in the block to avoid rewrite ObjectClass.
Syntax :123With ObjectExpression[Statements]End
[ObjectExpression] : Required. An expression that evaluates to an object or class/struct
[Statements] : Optional. One or more statements between ‘With’ and ‘End’ that may refer to members of an objects that’s produced by the evaluation of [ObjectExpression]
[End] : Required. Terminates the definition of the “With” block
Monkey2 example :123456789101112131415161718192021222324252627282930313233343536Class CustomerField Name:StringField City:StringField URL:StringField Comments:=New List<String>EndFunction AddCustomer() '// without 'With statement'Local theCustomer:= New CustomertheCustomer.Name = "John Doe"theCustomer.City = "Unknown"theCustomer.URL = "http://www.john.doe"theCustomer.Comments.Add( "First comment." )theCustomer.Comments.Add( "Second comment." )If theCustomer.City = "" Then theCustomer.City = "Earth"EndFunction AddCustomer() '// with 'With statement'Local theCustomer:= New CustomerWith theCustomer '// Each 'With' block executes a series of statement one a single object.Name = "John Doe".City = "Unknown".URL = "http://www.john.doe"With .Comments '// Within the nested 'With' statement, the syntax refer to the inner object.Add( "First comment." ).Add( "Second comment." )EndIf .City = "" Then .City = "Earth" '// If statementEndEndAugust 18, 2017 at 8:45 am #9900
Isnt this the same and less verbose?1234567[crayon-5a5f8ff14d3e8953314003 inline="true" ]Local theWindow:= New EntryWindowtheWindow.InfoLabel.Content = "This is a message."theWindow.InfoLabel.Foreground = Brushes.DarkSeaGreentheWindow.InfoLabel.Background = Brushes.LightYellowtheWindow.Title = "The Form Title"theWindow.Show()August 18, 2017 at 8:54 am #9901
Obviously this has a real interest if there are many members called for the same object, or calling the members of these nested objects. In use it will always be much less verbose.August 18, 2017 at 10:15 am #9902
mmm, good concept.
Really bad code example though.
This is a much better example of using that class concept:1234567891011121314151617181920Class CustomerField _name:StringField _city:StringField _url:StringField _comments:=New List<String>method AddCustomer( name:string, city:string, url:string )_name = name_url = urlIf city = "" Then_city = "Earth"Else_city = cityEnd If_comments.Add( "First comment." )_cComments.Add( "Second comment." )End methodEnd Class
You don’t need to use ‘with’
Just initialise with new and use add. there is no need to have functions cluttering up everything…August 18, 2017 at 10:02 pm #9905
The right subject is to request to integrate “With” statement in Monkey2 language, no how to use an equivalent. 🙂
Do you think this is possible to add this in Monkey2 simply?August 19, 2017 at 9:47 pm #9929
Having two ways of doing something makes a language more complicated not simpler.
If you adopt use of methods for modifying objects and avoid any reference of fields from outside an objects implementation the programs you create will be easier to expand and maintain.August 20, 2017 at 8:56 am #9943
The “With … End” statement is considered as a “helper” to have a clearer and less verbose source code, neither more nor less, it does not modify the control structures. A bit like the use of “Alias” which allows to “simplify” a type of variable or function. For example, you can write “Rect <Float>” to “Rectf” in your “Rect <Float>” and with “Alias”, and nothing prevents you from writing “Rect <Float>” elsewhere of your application. There are two different ways to use it but quite compatible.
- You define an “ObjectClass” with “With” keyword as the current “Object” to use in the block
- Inside the “With” block, you use (dot) “.MyMethod or .MyProperty” directly without rewriting your declared “With”
You must be logged in to reply to this topic.