ETSI's Bug Tracker |
Anonymous | Login | Signup for local Mantis account | 25-05-2024 13:24 IST |
Main | My View | View Issues | Change Log | Roadmap | Monitor project |
View Revisions: Issue #7870 | [ All Revisions ] [ Back to Issue ] | ||
Summary | 0007870: Allow definition of class properties | ||
Revision | 27-08-2019 13:04 by Jacob Wieland - Spirent | ||
Description | A class property is a class member which is referenced like a record field for reading and writing with the dotted notation, but implemented via getter and setter functions that are provided in the definition of the property (allowing value checking/normalization/conversion when setting a value and on-the-fly computation when getting the value). In C#, this looks like this: [<visibility>] <type> <identifier> { [<getter>] [<setter>] } [= <initial_value>]; <getter> and <setter> have different possible syntaxes: - [<visibility>] get { <body_returning value> } - [<visibility>] set { <body assigning value> } - [<visibility>] get => <result_value>; - [<visibility>] set => <value_assignment>; - [<visibility>] get; - [<visibility>] set; In the case that there is only a getter there is also a short version <visibility> <type> <identifier> => <result_value>; Thus, I propose the following syntaxes for TTCN-3 to define properties. var <template_modifier> <type> @property <modifiers> {<identifier> [<property_body>] [= <initial_value>] [","]}+ [";"] <property_body> has the following structure: "=>" <expression> | "{" (<getter> [<setter>] | <setter> [<getter>) "}" <getter> has the following structure: <modifiers> @get ("=>" <expression> [";"] | "{" { <statement> }+ "}") <setter> has the following structure: <modifiers> @set ("=>" <assignment> [";"] | "{" { <statement> } "}") We don't allow the { get; set; } syntax for automatic properties (without backing extra fields), instead, a @property variable without a <property_body> is treated as an automatic read/write property. When a property variable with a given <setter> is accessed on the left hand side of an assignment, the setter statements will be evaluated with the formal parameter 'value' being bound to the actual parameter of the right-hand-side. Accessing a non-automatic property value without a given <setter> on the left-hand side shall result in an error. When a property variable with a given <getter> is evaluated on the right hand side, the getter statements or expression will be evaluated and the resulting value will be the result of the evaluation. Accessing a non-automatic property value without a given <getter> on the right-hand side shall result in an error. Properties can be declared public and can be overridden by subclasses with the same visibility restrictions as for methods. Properties as well as their getters and setters can also be declared @abstract inside @abstract classes. If a property is declared abstract, the getter and/or setter does not need a body. A property, as well as getters and setters can also be declared @final. If a property is declared @final then neither the getter nor the setter can be overridden, otherwise, if a getter or setter itself is declared @final, then it shall not be overridden. Properties can be overridden by subclasses if they are not declared @final. EXAMPLE: type class MyPropertyClass { private var charstring v_color; // read-write property with backing variable public var charstring @property color { // getter in short form @get => v_color; @set { if (isColor(value)) { v_color := value } else { raise v_color&" is not a color" } } } // read-only property with computed value public var RGB @property rgb { // getter in long form @get { return computeRGB(v_color) } } // automatic property with initial value public var float @property size := 0.0; // read-only property with derived result in short form public var float @property square => size*size; // write-only property with short form public var float @property half_size { @set => size := half_size*2.0; } } |
||
Revision | 27-08-2019 11:48 by Jacob Wieland - Spirent | ||
Description | A class property is a class member which is referenced like a record field for reading and writing with the dotted notation, but implemented via getter and setter functions that are provided in the definition of the property (allowing value checking/normalization/conversion when setting a value and on-the-fly computation when getting the value). In C#, this looks like this: [<visibility>] <type> <identifier> { [<getter>] [<setter>] } [= <initial_value>]; <getter> and <setter> have different possible syntaxes: - [<visibility>] get { <body_returning value> } - [<visibility>] set { <body assigning value> } - [<visibility>] get => <result_value>; - [<visibility>] set => <value_assignment>; - [<visibility>] get; - [<visibility>] set; In the case that there is only a getter there is also a short version <visibility> <type> <identifier> => <result_value>; Thus, I propose the following syntaxes for TTCN-3 to define properties. var <template_modifier> <type> @property {<identifier> [<property_body>] [= <initial_value>] [","]}+ [";"] <property_body> has the following structure: "=>" <expression> | "{" (<getter> [<setter>] | <setter> [<getter>) "}" <getter> has the following structure: @get ("=>" <expression> [";"] | "{" { <statement> }+ "}") <setter> has the following structure: @set ("=>" <assignment> [";"] | "{" { <statement> } "}") We don't allow the { get; set; } syntax for automatic properties (without backing extra fields), instead, a @property variable without a <property_body> is treated as an automatic read/write property. When a property variable with a given <setter> is accessed on the left hand side of an assignment, the setter statements will be evaluated with the formal parameter 'value' being bound to the actual parameter of the right-hand-side. Accessing a non-automatic property value without a given <setter> on the left-hand side shall result in an error. When a property variable with a given <getter> is evaluated on the right hand side, the getter statements or expression will be evaluated and the resulting value will be the result of the evaluation. Accessing a non-automatic property value without a given <getter> on the right-hand side shall result in an error. EXAMPLE: type class MyPropertyClass { private var charstring v_color; // read-write property with backing variable public var charstring @property color { // getter in short form @get => v_color; @set { if (isColor(value)) { v_color := value } else { raise v_color&" is not a color" } } } // read-only property with computed value public var RGB @property rgb { // getter in long form @get { return computeRGB(v_color) } } // automatic property with initial value public var float @property size := 0.0; // read-only property with derived result in short form public var float @property square => size*size; // write-only property with short form public var float @property half_size { @set => size := half_size*2.0; } } |
MantisBT 1.2.14 [^] Copyright © 2000 - 2024 MantisBT Team |