Property, remarks and issues

Forum for discussion about the documentation project.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property, remarks and issues

Post by fxm »

About your property 'SegmentUDT.LengthInputStyle2() ByRef As UInteger'

Warning for functions returning by reference:
- If the function result is never set in the function body, the compiler outputs an error message.
- But if the function result is not set when exiting through a specific branch of the function body, the compiler outputs not any error message and returns (I think) per default a dereferenced NULL pointer that almost always induces a crash (because as the function is defined with return by reference, can be assumed that the return value of the function is used in the calling code).

Example (compiling with option -exx):

Code: Select all

Function fRef (Byref I As UInteger) Byref As UInteger
  If I <> 0 Then Return I
End Function


Dim I As UInteger
Print @I
Print

I = 456
Print @(fRef(I)), fRef(I)
I = 0
Print @(fRef(I)), fRef(I)

Sleep

Code: Select all

1244912

1244912       456
0
Aborting due to runtime error 12 ("segmentation violation" signal) in d:\Documen
ts and Settings\t0003830\Mes documents\FBIde0.4.6r4_fbc1.03.0\FBIDETEMP.bas::()
No problem for functions returning by value:
- If the function result is never set in the function body, the compiler outputs a warning message only.
- But if the function result is not set when exiting through a specific branch of the function body, the compiler outputs not any warning message and always returns a temporary object but non-constructed (as if using the object initializer '= Any').
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Property, remarks and issues

Post by counting_pine »

Regarding combination operators on properties, v1ctor commented on a feature request for that here:
http://sourceforge.net/p/fbc/feature-requests/106/

I argued against it somewhere on a similar request, but I can't find it now.

For what it's worth, I used to think the same thing, but something changed my mind:
1. It's possible in .NET languages, and it makes some things significantly easier ('MyControl.Width += 10')
2. There is an informal understanding there that properties are cheap to execute - they are automatically executed, for example, while debugging if you peek at the contents of a variable. If the understanding is there that properties should not take a long time or cause side-effects, then arguably the compiler should not prevent such things. In any case, they are less "evil" than byref function returns.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Property, remarks and issues

Post by marcov »

IMHO the core point of properties is to abstract function and field syntax.

Setting arbitrary limits on properties (e.g. simpleness of the function) therefore IMHO defeats the purposes of properties.

In FPC we allowed by ref pseudo function INC and DEC at first, but disabled it later. (since 2.4.0 I think)
Last edited by marcov on Jun 17, 2015 14:07, edited 2 times in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property, remarks and issues

Post by fxm »

Tourist Trap, about your code example (SegmentUDT), I think that differentiate two constructors by only the numeric datatype of one parameter, either Single or Uinteger, is very risked (1 or 1.0 does not call the same constructor):

Code: Select all

Type SegmentUDT
    Declare Constructor()
    Declare Constructor(ByVal X0 As Single , _
                        ByVal Y0 As Single , _
                        ByVal X1 As Single , _
                        ByVal Y1 As Single )
    Declare Constructor(ByVal X0 As Single , _
                        ByVal Y0 As Single , _
                        ByVal Length As UInteger, _
                        ByVal Angle As Single )
    '.....   
    Private :
     As Single _x0
     As Single _y0
     As Single _x1
     As Single _y1
     As UInteger _length
     As Single _Angle
End Type
'.....
Dim As SegmentUDT seg = (10, 20, 30, 40) '???

Perhaps rather use incompatible types, by predefining for example a 'PointUDT':

Code: Select all

Type PointUDT
    As Single _x
    As Single _y
End Type

Type SegmentUDT
    Declare Constructor ()
    Declare Constructor (ByVal P0 As PointUDT, _
                         ByVal P1 As PointUDT)
    Declare Constructor (ByVal P0 As PointUDT, _
                         ByVal Length As Single, _
                         ByVal Angle As Single)
    '.....   
    Private :
        As PointUDT _p0
        As PointUDT _p1
        As Single _length
        As Single _Angle
End Type
'.....
Dim As SegmentUDT seg = (Type<pointUDT>(10, 20), Type<pointUDT>(30, 40))


Other remark: Cos(), Sin(), Atan2() works in radians and not in degrees!
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property, remarks and issues

Post by fxm »

About your two properties returning by reference
SegmentUDT.LengthInputStyle2() ByRef As UInteger
SegmentUDT.AngleInputStyle2() ByRef As Single

These two properties (returning by reference) do not fully work as you wish when used to assign new value to Length or Angle:
- In the property body, the second point (._x1, ._y1) will be updated but with the unmodified value of Length or Angle, because the modification occurs only after the property return in the calling line (and not after the line 'Property = ...' in the property body).
- An horrible workaround would be that user does twice the assignment (with the same value obviously)!!!

Example of same behavior with function returning by reference:

Code: Select all

Dim Shared As Integer I

Function f () Byref As Integer
  Function = I
  Print I
End Function

f() = 1
Print I
Print
f() = 1
Print I

Sleep

Code: Select all

 0
 1

 1
 1
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Property, remarks and issues

Post by fxm »

counting_pine wrote:Regarding combination operators on properties, v1ctor commented on a feature request for that here:
http://sourceforge.net/p/fbc/feature-requests/106/

I argued against it somewhere on a similar request, but I can't find it now.

For what it's worth, I used to think the same thing, but something changed my mind:
1. It's possible in .NET languages, and it makes some things significantly easier ('MyControl.Width += 10')
2. There is an informal understanding there that properties are cheap to execute - they are automatically executed, for example, while debugging if you peek at the contents of a variable. If the understanding is there that properties should not take a long time or cause side-effects, then arguably the compiler should not prevent such things. In any case, they are less "evil" than byref function returns.
I found it!
See at http://sourceforge.net/p/fbc/bugs/632/:
- XUnwichtig's report
- Matthew's comment
- fxm's comment, with a link to forum
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Property, remarks and issues

Post by Tourist Trap »

fxm wrote: (..)
Thanks for all the remarks. About byref returned properties, I'm still wondering exactly how all this work but I will come back to this later, since I wont have too much time those next days. Of course I'm taking record of your precious remarks by now. Just the return byref will require more time to understand fully.
fxm wrote:I think that differentiate two constructors by only the numeric datatype of one parameter, either Single or Uinteger, is very risked (1 or 1.0 does not call the same constructor):
(...)
Perhaps rather use incompatible types, by predefining for example a 'PointUDT':
It's very important remark. However I would maybe not introduce another type to differentiate the constructors. I would prefere to introduce an Enum like :

Code: Select all

Type ...
   Enum _ConstructorMode
        _constructionBy2points = 1
        _contructionByStartPointLengthAngle = 2
  (...)
   Enum
  Declare Constructor (Byval Mode As _ConstructorMode, ......)

End Type
Anyway it's a very important thing that you've pointed here. I often have made this mistake.
Post Reply