Why is extending builtin classes not allowed?

Godot Version

V4

Question

Does anyone know why I cannot extend built in classes like String or Array.

For example if I want to make my own inner class and extend from String, I would not be allowed to do it. Any alternatives if I want to do it.

Builtins like Array or String is not allowed

class Result extends Array:
	var error_code: int=0
	var error_description: String=""

	func to_string2()->String:
		return "somevalue"

This is allowed

class Result extends Object:
	var error_code: int=0
	var error_description: String=""
	
	func to_string2()->String:
		return "somevalue"

Not a pro at this, but I’ll have to guess that it’s because they are implemented in C++ and compiled with the engine.

I’d work around this by just making an Array variable and manipulating it how I want within the class. It’s not as pretty as just not having to type in that extra variable, but it does its job.

String and Array are not classes. They’re types, which cannot be extended the way classes can.

3 Likes