Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overloading functions by return type #78

Open
sirisian opened this issue Dec 5, 2022 · 0 comments
Open

Allow overloading functions by return type #78

sirisian opened this issue Dec 5, 2022 · 0 comments

Comments

@sirisian
Copy link
Owner

sirisian commented Dec 5, 2022

This apparently had performance concerns in early programming languages, but a few languages have implemented it including Swift and Haskell(?).

function F():uint32 {
  return 10;
}
function F():string {
  return "10";
}
// F(); // TypeError: Ambiguous signature for F. Requires explicit left-hand side type.
const a:string = F(); // "10"
const b:uint32 = F(); // 10

function G(a:uint32) {
  return a;
}
G(F()); // 10

function H(a:uint8) { }
function H(a:string) { }
// H(F()); // TypeError: Ambiguous signature for F. Requires explicit left-hand side type.

It's possible for an engine to have type inference that supports:

const a = F();
G(a); // a must be uint8, thus F():uint8 is the only valid signature.

To what extent should this inference be supported?

Why even support such a complex language feature?

In other languages you'll have people write getString() and getUInt8() or if they support generics get<string>() and get<uint8>(). This seems fine at first glance until you think about operators. Take SIMD operators, represented here by their intrinsic, that can return both a vector register or mask:

__m128i _mm_cmpeq_epi32 (__m128i a, __m128i b)
__mmask8 _mm_cmpeq_epi32_mask (__m128i a, __m128i b)

Notice Intel differentiates signatures by adding _mask. When translated to real types with operators they are identical however:

const something = int32x4(0, 1, 2, 3) < int32x4(0, 1, 3, 2);

But if we overload return types we can support both signatures:

const a:int32x4 = int32x4(0, 1, 2, 3) == int32x4(0, 1, 3, 2);
const b:boolean8 = int32x4(0, 1, 2, 3) == int32x4(0, 1, 3, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant