Skript icon indicating copy to clipboard operation
Skript copied to clipboard

Function overloading

Open Efnilite opened this issue 10 months ago • 7 comments

Description

Adds function overloading.

Function overloading allows two functions with the same name to be registered, as long as

  • their parameter types differ, or
  • the amount of arguments differ.

An example:

function overloaded(x: int, y: int) :: int:
	return 1

function overloaded(x: int, y: int, z: int) :: int:
	return 2

function overloaded(x: text, y: text) :: int:
	return 3

function overloaded(x: text, y: int, z: int) :: int:
	return 4

FunctionRegistry

Description To accomplish this, a new FunctionRegistry class has been added, which is only visible to the function package. This holds all signatures and functions by a namespace. A namespace is either the global namespace, for global functions, or a script, in which local functions can be registered.

Functions and signatures are differentiated by a FunctionIdentifier, which holds the function's name and the types of its arguments.

Why are the methods in the Functions class not deprecated? In the future, I plan on adding function parameter specification by name, e.g. location(x=10,y=10,z=10,world="world"). When that has been added, there will, in my opinion, be enough future-proofing, thus the FunctionRegistry class can replace the Functions class' functionality. Overloaded functions will not be visible to Functions.

Behaviour

When a function has an ambiguous type in one of its arguments, it will attempt to match based on the other arguments. Therefore, overloaded(1, {_none}) and overloaded({_none}, 2) will still call the correct overloaded, as the function overloaded(int, int) is the only one that can be matched here.

When a function has multiple implementations, and it can't be decided which one to use (due to variables for example), a parse-time error will be thrown.

image

Like other languages, functions cannot be differentiated by return type.


Target Minecraft Versions: any Requirements: none Related Issues: #1185, #2993

Efnilite avatar Mar 30 '25 14:03 Efnilite

"Try clarifying the type of the arguments"

How?

sovdeeth avatar Apr 01 '25 22:04 sovdeeth

"Try clarifying the type of the arguments"

How?

I was thinking literal type clarification but I guess that's only for literals

Efnilite avatar Apr 01 '25 22:04 Efnilite

"Try clarifying the type of the arguments"

How?

I was thinking literal type clarification but I guess that's only for literals

You could evaluate at runtime, which could be very flexible but also possibly confusing to users in some cases. store the candidates and pick at runtime

sovdeeth avatar Apr 01 '25 22:04 sovdeeth

I think I prefer a more limited system that errors at parse time over one that may cause runtime errors. Wdyt?

Efnilite avatar Apr 02 '25 08:04 Efnilite

Why not just use ExprValueWithin to clarify the type?

Burbulinis avatar Apr 02 '25 12:04 Burbulinis

Why not just use ExprValueWithin to clarify the type?

ahh good point

sovdeeth avatar Apr 02 '25 21:04 sovdeeth

I am really hyped for this one ngl

sverinn avatar Apr 05 '25 20:04 sverinn