How do I pass the file's type as a function parameter?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aristonaut

I am trying to write a custom copy-data function. I would like to give it a stronger typing so I can get the auto-complete. In other words, I have this:

# res://MyClass.gd
func copyFrom( other ):
    value = other.value
    # ...

but would like this:

# res://MyClass.gd
func copyFrom( other : MyClass ):
    value = other.value
    # ...

I have tried a few things with these frustrating findings:
It looks like you can’t preload a class in it’s own file.
It looks like load results can’t be made const, so they can’t be used as a type, (I think?)
I would add class_name but that seems to give me all kinds of circular errors if I try to use the file anywhere else.

There may not be a way to do it right now (3.1), and that’s fine, too. I just would like to know the answer if it is possible.

:bust_in_silhouette: Reply From: MonsterVial

Looks like it may be possible in v4 to use a class_name in it’s own file, but for now it isn’t doable. See

That issue has a few workaround suggestions. It seems that in 3.1.2+ you can sometimes use as or is with the class_name. (Like assert( other is MyClass )) That seems finicky and other files that uses that class can show errors and have trouble getting dependencies right. I think I’ll just leave it all dynamic typed until v4… insert the usual joke here.

Seems like this is the expected behavior.

# res://MyClass.gd
func copyFrom( other ):
    if other is MyClass:
        value = other.value
    elif other is SomeOtherClass:
        pass
# ...

All other kinds of class compare or use cases returns circular errors like the OP said

Wakatta | 2021-01-04 20:34