About Monkey 2 › Forums › Monkey 2 Development › Properties in interfaces
This topic contains 4 replies, has 3 voices, and was last updated by 
 Samah 2 years, 10 months ago.
- 
		AuthorPosts
 - 
		
			
				
June 3, 2016 at 2:16 pm #875
So, how do I define a property in an interface, but enforce that it must or must not have a setter? In Monkey-X I could explicitly define one or both, but MX2 has the property bunched into a single block.
Edit: Monkey-X example:
Monkey1234567891011121314151617Interface FooMethod Prop:Int() PropertyMethod Prop:Void(value:Int) PropertyEndClass Bar Implements FooMethod Prop:Int() PropertyReturn 123EndMethod Prop:Void(value:Int) Property' set somethingEndEndLocal foobar:Foo = New Barfoobar.Prop = 1 ' all goodMonkey 2 example?
Monkey123456789101112131415Interface FooProperty Prop:Int()' how do i define a setter?EndClass Bar Implements FooProperty Prop:Int()Return 123Setter(value:Int)' set somethingEndEndLocal foobar:Foo = New Barfoobar.Prop = 1 ' error, compiler doesn't know that Foo.Prop has a setterJune 3, 2016 at 8:31 pm #883Just leave out the ‘End’…
Monkey123456Interface IProperty P:Int()Setter( p:Int )EndJune 3, 2016 at 11:17 pm #884Thanks. It seems a bit awkward that you need to define an argument to the setter. Languages like C# and Swift give you an implicit variable called newValue
I wish it had a didSet/ willSet pair too.June 3, 2016 at 11:50 pm #885Leave out the ‘End’…. which ‘End’?
Getter/Setter:
Monkey123456789101112131415161718192021222324#Import "<std>"#Import "<mojo>"Interface FooProperty Prop:Int()Setter( p:Int )EndClass Bar Implements FooField value:IntProperty Prop:Int()Return valueSetter(value:Int)Self.value = valueEndEndFunction Main()Local foobar:Foo = New BarPrint("foobar.Prop = " + foobar.Prop)foobar.Prop = 1Print("foobar.Prop = " + foobar.Prop)EndGetter Only:
Monkey12345678910111213141516171819#Import "<std>"#Import "<mojo>"Interface FooProperty Prop:Int()EndClass Bar Implements FooField value:IntProperty Prop:Int()Return valueEndEndFunction Main()Local foobar:Foo = New BarPrint("foobar.Prop = " + foobar.Prop)EndDid I miss something here?
June 4, 2016 at 3:16 am #887@therevills: That’s basically what I wanted.
Edit: Ooohhh putting an @ creates a link to the user’s profile. - 
		AuthorPosts
 
You must be logged in to reply to this topic.