ABSTRACT doesn't allow type aliases?

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

Re: ABSTRACT doesn't allow type aliases?

Post by Tourist Trap »

fxm wrote:KeyPgThis → fxm [Added a link to BYREF (parameters) in the Description paragraph]
It's short but useful because it sends the reader that may face problems to the pages where he will find the answers. The Byref Parameter page itself contain all the links needed.

Thanks again.

ps:
The Byref page has this note:
Note: A constant or a literal expression can also be passed to such a procedure (which gets by reference), but they are obviously not modifiable. In that case, the compiler passes by reference a temporary variable initialized with the constant or the literal expression.
I'm not sure, are we facing this case with THIS?

pps:
Sorry but I think I have a last question on a detail. In the THIS doc page, we read:
The only times when you need to qualify member names with This is when the member name is hidden, for example, by a local variable or parameter.
I'm not sure here also, but officially shouldn't we say "shadowed"? Or "masked"? I'm not a perfect english reader but I think I remember of something similar in the VB documentation.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Post by fxm »

'This' is neither a constant nor a literal expression, because it is a (hidden) parameter (from a passed argument that is the instance of the object).

KeyPgByref → fxm [Wording]
Last edited by fxm on Jan 12, 2019 13:04, edited 1 time in total.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Post by fxm »

Yes, in the computer literature, one talks rather of "variable shadowing" and "name masking":
KeyPgThis → fxm [Wording]
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Default property alias

Post by Tourist Trap »

fxm wrote:Yes, in the computer literature, one talks rather of "variable shadowing" and "name masking":
KeyPgThis → fxm [Wording]
Ok, so now this point has been improved. I made some new research on how we may hide the function that realizes some flavour of default property.

Code: Select all

#macro _DEFINE(Instancename)
    #define Instancename##Self    Instancename.Self().Default
#endMacro
#macro _UNDEFINE(Instancename)
    #undef Instancename##Self 
#endMacro

#macro _UDTCONSTRUCTOR(InstanceName, TypeName)
    dim as TypeName     InstanceName
    #ifnDef Instancename##Self
        _DEFINE(InstanceName)
    #endIf    
#endMacro
#macro _UDTDESTRUCTOR(InstanceName, TypeName)
    #ifnDef Instancename##Self
        _UNDEFINE(InstanceName)
    #endIf    
#endMacro

type UDT
    declare constructor()
    declare destructor()
    declare property Default(as integer) as integer
    declare function Self() byref as UDT
    as integer  _memberarray(any)
end type
constructor UDT()
    redim THIS._memberarray(1 to 10)
    for i as integer = lBound(THIS._memberarray) to uBound(THIS._memberarray)
        THIS._memberarray(i) = i
    next i
end constructor
destructor UDT()
    _UDTDESTRUCTOR(uuu, UDT)
end destructor
property UDT.Default(Index as integer) as integer
    return THIS._memberarray(Index)
end property
function UDT.Self() byref as UDT
    return THIS
end function

_UDTCONSTRUCTOR(uuu, UDT)

? uuuSelf(10)

_UDTDESTRUCTOR(uuu, UDT)


Here the variable name is uuu, and I would like to access the Default property without writing uuu.Default().
Said differently, I would like to call the Default prop like this: uuu() .
In the code above I can write uuuSelf() instead.

Do someone see a better workaround?

Question:
Why can't I use a underscore with the macro string making operator ## ?

Code: Select all

#macro _DEFINE(Instancename)
    #define Instancename##_    Instancename.Self().Default
#endMacro
#macro _UNDEFINE(Instancename)
    #undef Instancename##_
#endMacro

#macro _UDTCONSTRUCTOR(InstanceName, TypeName)
    dim as TypeName     InstanceName
    #ifnDef Instancename##_
        _DEFINE(InstanceName)
    #endIf    
#endMacro
#macro _UDTDESTRUCTOR(InstanceName, TypeName)
    #ifnDef Instancename##_
        _UNDEFINE(InstanceName)
    #endIf    
#endMacro

type UDT
    declare constructor()
    declare constructor(byref as const string)
    declare destructor()
    declare property Default(as integer) as integer
    declare function Self() byref as UDT
    as integer  _memberarray(any)
end type
constructor UDT()
    redim THIS._memberarray(1 to 10)
    for i as integer = lBound(THIS._memberarray) to uBound(THIS._memberarray)
        THIS._memberarray(i) = i
    next i
end constructor
destructor UDT()
    _UDTDESTRUCTOR(uuu, UDT)
end destructor
property UDT.Default(Index as integer) as integer
    return THIS._memberarray(Index)
end property
function UDT.Self() byref as UDT
    return THIS
end function

_UDTCONSTRUCTOR(uuu, UDT)

? uuu_(10)

_UDTDESTRUCTOR(uuu, UDT)
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ABSTRACT doesn't allow type aliases?

Post by fxm »

Tourist Trap wrote:Here the variable name is uuu, and I would like to access the Default property without writing uuu.Default().
Said differently, I would like to call the Default prop like this: uuu().
What a funny idea!
Tourist Trap wrote:Why can't I use a underscore with the macro string making operator ## ?
See the very recent post viewtopic.php?p=257036#p257036.
(works for example with a double underscore)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: ABSTRACT doesn't allow type aliases?

Post by Tourist Trap »

fxm wrote:
Tourist Trap wrote:Here the variable name is uuu, and I would like to access the Default property without writing uuu.Default().
Said differently, I would like to call the Default prop like this: uuu().
What a funny idea!
This exists in VB. And I will add it to my collection of aliasing technics, if you remember :)
viewtopic.php?f=7&t=25182&p=226090&hilit=alias#p226090
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: ABSTRACT doesn't allow type aliases?

Post by sancho3 »

tourist wrote:Said differently, I would like to call the Default prop like this: uuu() .
In the code above I can write uuuSelf() instead.
This is called a default property and has been a part of VB from very early on.
You can replicate this using cast and let operators except as shown in your code where there is an array.
Ideally the () array access operator will be able to be overloaded, This is not the case right now.
So you can bastardize the [] pointer index operator and use it.

Code: Select all

#macro _DEFINE(Instancename)
    #define Instancename##Self    Instancename.Self().Default
#endMacro
#macro _UNDEFINE(Instancename)
    #undef Instancename##Self
#endMacro

#macro _UDTCONSTRUCTOR(InstanceName, TypeName)
    dim as TypeName     InstanceName
    #ifnDef Instancename##Self
        _DEFINE(InstanceName)
    #endIf   
#endMacro
#macro _UDTDESTRUCTOR(InstanceName, TypeName)
    #ifnDef Instancename##Self
        _UNDEFINE(InstanceName)
    #endIf   
#endMacro

type UDT
    declare constructor()
    declare destructor()
    declare property Default(as integer) as integer
    declare function Self() byref as UDT
    as integer  _memberarray(any)
	 Declare Operator [](Byval rhs As integer) Byref As Integer 
end type
constructor UDT()
    redim THIS._memberarray(1 to 10)
    for i as integer = lBound(THIS._memberarray) to uBound(THIS._memberarray)
        THIS._memberarray(i) = i
    next i
end constructor
destructor UDT()
    _UDTDESTRUCTOR(uuu, UDT)
end destructor
property UDT.Default(Index as integer) as integer
    return THIS._memberarray(Index)
end property
function UDT.Self() byref as UDT
    return THIS
end Function
Operator UDT.[](Byval rhs As integer) Byref As Integer 
	'
	Return this._memberarray(rhs) 
End Operator

_UDTCONSTRUCTOR(uuu, UDT)

? uuuSelf(10)

uuu[1] = 12
? uuu[1]


_UDTDESTRUCTOR(uuu, UDT)
Sleep 
Edit:
There is no reason for the parameter to be byref so I changed the parameter to byval.
Also note that this only works for single dimension arrays.
There is the down side that this operator is not being used for its intended purpose. The casual observer of the code may be confused.
Last edited by sancho3 on Jan 13, 2019 19:12, edited 1 time in total.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: ABSTRACT doesn't allow type aliases?

Post by Tourist Trap »

sancho3 wrote: You can replicate this using cast and let operators except as shown in your code where there is an array.
Ideally the () array access operator will be able to be overloaded, This is not the case right now.
Nice sancho, I always forget the square brackets can be overloaded. Default properties are a good way to shorten the access to a frequent or complicated expression. Alternatively we can use a define each time locally but it needs to be undef or tested with a ifndef, and so on. So it's code typing when most often we want to type less (without using obscure overshort names).
Thanks!
Post Reply