Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[Feature] Add an array generic type

Open Ph0enixKM opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe. The problem is that sometimes a person might want to match any Array type when declaring a function parameter.

fun len(array: []): Num {
    ...
}

Describe the solution you'd like

  • Add the [] syntax in Types
  • Handle the [] type only in function parameter types.

Describe alternatives you've considered I've considered function overloading but it seems to be an overkill.

Additional context This way we can more precisely specify the context and use of functions.

Ph0enixKM avatar Aug 06 '24 17:08 Ph0enixKM

what about adding explicit Any type and every time you need to put a generic type anywhere you just use Any? (including arrays)

b1ek avatar Aug 08 '24 13:08 b1ek

Yeah.. I've been thinking about this... if this is a good idea to introduce such type. TypeScript has done it because JS enforced it. Aaaand... in any real project you're advised not to use it. So this was a proposition that would not lead people to bad patterns. If you want to use Any type - then just don't specify any type. What do you think?

Ph0enixKM avatar Aug 08 '24 14:08 Ph0enixKM

How about we add generics instead.

fn foo<T>(array: [T]) {

}

Ph0enixKM avatar Aug 08 '24 15:08 Ph0enixKM

I'd start with the generic array, as it is simpler to implement and to use. Also we don't have to limit it to parameters, it might be useful as a return type, too.

Then, later on, we can extend this syntax to also allow named generics such as fn first<T>(arr: [T]): T. Except I'd also look for a better syntax that won't require defining them before using, like fn first(arr: ['T]): 'T.

mks-h avatar Sep 02 '24 18:09 mks-h

I'd start with the generic array, as it is simpler to implement and to use. Also we don't have to limit it to parameters, it might be useful as a return type, too.

Then, later on, we can extend this syntax to also allow named generics such as fn first<T>(arr: [T]): T. Except I'd also look for a better syntax that won't require defining them before using, like fn first(arr: ['T]): 'T.

I like this 'T syntax

KrosFire avatar Sep 03 '24 16:09 KrosFire

I love that idea of 'T syntax @mks-h ! Great job!

Ph0enixKM avatar Sep 03 '24 17:09 Ph0enixKM