[SOLVED] with keyword calls class constructor

General FreeBASIC programming questions.
Post Reply
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

[SOLVED] with keyword calls class constructor

Post by shadow008 »

Code: Select all

type thing
    
    enum myEnum
        STUFF = 1
    end enum
    
    dim value as integer
    
    declare constructor()
    
    declare function setDefault() as integer
    
end type

constructor thing()

    print "setting constructor"
    this.value = this.setDefault()

end constructor

function thing.setDefault() as integer
    
    dim retVal as integer
    
    print "entering with"
    with thing
        print "SUCCESS"
        retVal = .STUFF
    end with
    
    return retVal
    
end function

dim test as thing

sleep
In this example, I'm attempting to use the "with" keyword to allow quick access to the UDT's enum. Allowing for simply typing .STUFF vs typing thing.STUFF. In my actual use case, the type name is significantly longer than that, so this enhances readability by a good bit.
But, bizarrely, "with" is calling the constructor to the type, creating an infinite loop. Surely this is a bug?

I am using fb 1.09 compiler.
Last edited by shadow008 on Feb 26, 2023 20:01, edited 1 time in total.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: [BUG?] with keyword calls class constructor

Post by paul doe »

No bug:

Code: Select all

type thing
    
    enum myEnum
        STUFF = 1
    end enum
    
    dim value as integer
    
    declare constructor()
    
    declare function setDefault() as integer
    
end type

constructor thing()

    print "setting constructor"
    this.value = this.setDefault()

end constructor

function thing.setDefault() as integer
    
    dim retVal as integer
    
    print "entering with"
    with this
        print "SUCCESS"
        retVal = .STUFF
    end with
    
    return retVal
    
end function

dim test as thing

sleep
Just a typo.
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: [BUG?] with keyword calls class constructor

Post by shadow008 »

My apologies, I seem to have missed the most important part. The setDefault() function was static. 'this' cannot be used in that case.

Furthermore, using 'this' would only be a workaround as, to my understanding, 'with' is still calling the constructor regardless of whether the function is static or not.

Code: Select all

type thing
    
    enum myEnum
        STUFF = 1
    end enum
    
    dim value as integer
    
    declare constructor()
    
    declare static function setDefault() as integer
    
end type

constructor thing()

    print "setting constructor"
    this.value = thing.setDefault()

end constructor

static function thing.setDefault() as integer
    
    dim retVal as integer
    
    print "entering with"
    with thing
        print "SUCCESS"
        retVal = .STUFF
    end with
    
    return retVal
    
end function

dim test as thing

sleep
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [BUG?] with keyword calls class constructor

Post by fxm »

'with' does not call the constructor, but the next term 'thing' does because 'thing' so used is shortcut for 'thing()' which calls the default constructor to construct a new instance.

If you want a static function to access a specific instance, you must explicitly pass it to the function:

Code: Select all

type thing
    
    enum myEnum
        STUFF = 1
    end enum
    
    dim value as integer
    
    declare constructor()
    
    declare static function setDefault(byref t as thing) as integer
    
end type

constructor thing()

    print "setting constructor"
    this.value = thing.setDefault(this)

end constructor

static function thing.setDefault(byref t as thing) as integer
    
    dim retVal as integer
    
    print "entering with"
    with t
        print "SUCCESS"
        retVal = .STUFF
    end with
    
    return retVal
    
end function

dim test as thing

sleep
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: [SOLVED] with keyword calls class constructor

Post by shadow008 »

Haha this is news to me! I was under the impression that 'with' was simply doing namespacing, not instantiating a default of the class, as is in my example. But it makes sense now that you point it out.

Well the fix in my case is to decouple the function from the constructor, which works well enough. Thank you.
Post Reply