amber-lang / amber

💎 Amber the programming language compiled to Bash
https://amber-lang.com
GNU General Public License v3.0
3.9k stars 85 forks source link

[Feature] Add an array generic type #391

Closed Ph0enixKM closed 1 month ago

Ph0enixKM commented 2 months ago

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

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.

b1ek commented 2 months ago

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

Ph0enixKM commented 2 months ago

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 commented 2 months ago

How about we add generics instead.

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

}
mks-h commented 2 months ago

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.

KrosFire commented 2 months ago

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

Ph0enixKM commented 2 months ago

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