Trouble with Property returning by propName = value (solved)

General FreeBASIC programming questions.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 19, 2019 14:59

Hi,

I wouldn't have thought to face this rather disturbing situation. It seems that in some cases properties are not properly recognized. Or I miss the trick:

Code: Select all

type UDT extends OBJECT
    declare property Prop() as integer
end type

property UDT.Prop() as integer
    Prop = 1
end property

dim as UDT uuu
? uuu.Prop



type UDT2 extends OBJECT
    declare property Prop2(byval as integer)
    declare property Prop2(byval as integer) as integer
end type

property UDT2.Prop2(byval z as integer)
    '
end property

property UDT2.Prop2(byval z as integer) as integer
    Prop2 = 2
end property

dim as UDT2 vvv
? vvv.Prop2(2)

In the UDT2, I added the Prop3 SETTER because the compiler was complaining about it but it the GETTER with the parameter Z passed that I need to be called here.
The error message that in any case I can not understand is:
warning 13(0): Function result was not explicitly set

PROP2 = 2 is actually the function result.

So it's tricky. Maybe I have to use the version returning Byref, I don't know. If someone has any idea...

Thanks
Last edited by Tourist Trap on Jan 19, 2019 15:35, edited 1 time in total.
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value or related

Postby fxm » Jan 19, 2019 15:22

For a property-GETTER, use 'Property = x' or 'Return x' syntax.
Last edited by fxm on Jan 19, 2019 15:47, edited 1 time in total.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value or related

Postby Tourist Trap » Jan 19, 2019 15:25

fxm wrote:For a property-GETTER, use 'Property = x' or 'Return x' syntax.

Hello fxm,

Sure I would use RETURN, but in the true project, I have to clean-up a POINTER constructed with NEW just after I returned it:

Code: Select all

property X
   dim tri as TRIANGLE => new TRIANGE
   '...do stuff
   X = tri
   delete tri
end property

You see. It's tricky then.
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value or related

Postby fxm » Jan 19, 2019 15:29

Use 'Property = x'.
(it is similar to 'Function = x')
Last edited by fxm on Jan 19, 2019 15:47, edited 1 time in total.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value or related

Postby Tourist Trap » Jan 19, 2019 15:35

fxm wrote:Use 'Property = x'.
(it is similar to 'Function = x')

Ahhh :)
Thanks I totally forgot this variation.
So we have:
propname = returnvalue
property = returnvalue
or if no more code left after the return: return returnvalue.

It will do the job then, even if I made some other adhoc change to spend less time on this, it's for my game project and I'm very late!
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 19, 2019 15:38

@admin:
More generally the syntax'vvv.Prop2 = x' does not trig error!
(calling to the property-setter without specifying an index value)
Last edited by fxm on Jan 19, 2019 15:47, edited 2 times in total.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 19, 2019 15:42

fxm wrote:@admin:
More generally the syntax 'vvv.Prop2 = x' does not trig error!
(calling to the property-setter without specifying an index value)


This case maybe can be confusing:

Code: Select all

dim shared prop2 as integer

type UDT2 extends OBJECT
    declare property Prop2(byval as integer)
    declare property Prop2(byval as integer) as integer
end type

property UDT2.Prop2(byval z as integer)
    '
end property

property UDT2.Prop2(byval z as integer) as integer
    Prop2 = 2
end property

dim as UDT2 vvv
? vvv.Prop2(2)

This one goes even further:

Code: Select all

dim shared prop2 as integer

type UDT2 extends OBJECT
    declare property Prop2(byval as integer)
    declare property Prop2(byval as integer) as integer
end type

property UDT2.Prop2(byval z as integer)
    '
end property

property UDT2.Prop2(byval z as integer) as integer
    ..Prop2 = 5
    prop2 = 2
end property

dim as UDT2 vvv
? vvv.Prop2(2)
? prop2
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 19, 2019 15:48

Am I already asleep?

You defined a setter without index and a getter with index.
(so 'Prop2 = 2' call the setter)

For a getter with an index parameter, we cannot use the 'propertyName = x' syntax as for a function.
Use only 'Property = x' or 'Return x' syntax.
Last edited by fxm on Jan 19, 2019 16:21, edited 1 time in total.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 19, 2019 15:58

fxm wrote:Am I already asleep?

You defined a setter without index and a getter with index.

Ahah that's the problem with real life code examples.
In my project it's even worst, my property has also the same name as the type returned. I have namespaces so it's ok. In any case I'm simplifying in this project when it takes me time to rewrite. But in general that's things that may really happen. I've many code files with includes, I have shared variables, I have to write things with names very similar to types in order to know what I'm using because I have many files included .

But this is still true that the first error message leads to add a getter even if not wanted first. Maybe this is a case where we must use a function and not a property in general. I don't know.

fxm wrote:For a getter, we cannot use the 'propertyName = x' syntax as for a function.
Use only 'Property = x' or 'Return x' syntax.

You mean that this is forbidden to do this?

Code: Select all

type UDT2 extends OBJECT
    declare property Prop2(byval as integer) as integer
end type

property UDT2.Prop2(byval z as integer) as integer
    prop2 = z
end property
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 19, 2019 16:05

Yes (even if there is no setter defined), but only true for a getter with an index parameter.
Last edited by fxm on Jan 19, 2019 16:17, edited 2 times in total.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 19, 2019 16:11

fxm wrote:Yes (even if there is no setter defined).

You know fxm, that's why, in general I always name the setter parameter "Setvalue".
For example:

Code: Select all

type UDT2 extends OBJECT
    declare property Prop2(byval as integer) as integer
end type

property UDT2.Prop2(byval Setvalue as integer) as integer
    prop2 = Setvalue
end property

I would immediatly see then that there is something weird in this -> why here in general would I return a value?

Here I added a parameter because the triangle (that's not mine, it's a stonemonkey's piece of code) require finally a Buffer parameter (or a shared variable, or a passed argument, in general). I don't master it since it's not mine, so I tried to add this parameter lately. So leading to where we are ;)

Here it's a getter of the form:

Code: Select all

type UDT2 extends OBJECT
    declare property Prop2(byval as any ptr) as integer
end type

property UDT2.Prop2(byval Buffer as any ptr) as integer
    prop2 = getvalue(Buffer)
end property
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 19, 2019 19:56

I propose to add a note at the PROPERTY documentation page:
Note: An indexed get-Property can not use the 'fieldname=' syntax to return a value. For such an indexed get-Property, the 'property=' syntax (in addition to the 'return' syntax) is only the one allowed.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 19, 2019 20:04

fxm wrote:
Note: An indexed get-Property can not use the 'fieldname=' syntax to return a value. For such an indexed get-Property, the 'property=' syntax (in addition to the 'return' syntax) is only the one allowed.

I would rather say "a get-Property with parameters passed". Index maybe can be confusing with an integer parameter.
Otherwise I vote yes, good idea, thank you.
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Trouble with Property returning by propName = value (solved)

Postby fxm » Jan 19, 2019 20:15

New proposal:
Note: When a get-Property is defined with one 'index' parameter, the 'fieldname=' syntax can not be used to return a value. For such a get-Property, the 'property=' syntax (in addition to the 'return' syntax) is only the one allowed.
Tourist Trap
Posts: 2614
Joined: Jun 02, 2015 16:24

Re: Trouble with Property returning by propName = value (solved)

Postby Tourist Trap » Jan 19, 2019 20:22

fxm wrote:
Note: When a get-Property is defined with one 'index' parameter, the 'fieldname=' syntax can not be used to return a value. For such a get-Property, the 'property=' syntax (in addition to the 'return' syntax) is only the one allowed.

Fine, but then arises a question. What if I pass more than one parameter? This is still the same rule I think, or not? I'm not specifically encouraging to pass parameters in this context but it can occur of course, and someone may ask this some day.

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest