albertodemichelis / squirrel

Official repository for the programming language Squirrel
http://www.squirrel-lang.org
MIT License
913 stars 156 forks source link

_get doesn't trigger when no index is provided #233

Open oomek opened 3 years ago

oomek commented 3 years ago

I'm struggling with intercepting a _get call with no provided index. I would need to return one of the objects inside the class when only the instance name is used and pass it as a function parameter. Is there any way that would give such functionality, or some workaround maybe?

class myClass()
{
    object = null

    constructor()
    {
        object = objClass()
    }

    function _get( idx )
    {
        // this does not trigger
        return object
    }

    function _call( other )
    {
        return object
    }
}

local inst = myClass()

// this doesn't work
someFunction( inst )

// this works, but it's gonna break the compatibility
// with the scripts for our app already written by the end-users
someFunction( inst() )
RizzoRat commented 3 years ago

err, that would break semantics and , most important, totally breaks the accessibility of the instance. You could NEVER EVER access any member or function of that instance again, or not even be able to create an instance if _get would work that way you want. Example: class foo { function _get() { return 5 ; } //yes, we return an integer value here to epicly slap your face! function bar() { print("Called bar\n") ; }

local inst = foo() ; //would thow an error, as "foo" would return 5 and that translates to: // local inst = 5() so you would try to call an integer value. -> Crash

even if instantiation is done in another way, same applies for:

inst.bar() ; //what would 5.bar() result in? Yes, "Trying to call an integer value"