v icon indicating copy to clipboard operation
v copied to clipboard

Generics syntax discussion

Open medvednikov opened this issue 7 years ago • 44 comments

Generics are back thanks to @shsr04.

This time they are more powerful (for example, multiple types are supported).

You can see some examples here: https://github.com/vlang/v/blob/master/vlib/compiler/tests/generic_test.v

One big change is the type parameters being removed from the function call:

foo<int>(1)
foo<string>('str')
foo<User>(user)

becomes

foo(1)
foo('str')
foo(user)

To me it looks a bit like overloaded functions, which are not allowed in V. Of course the function is still the same, and does the same thing, unlike overloaded functions which can do anything depending on the type.

Also some functionality is lost. vweb uses the following construct to build an application object:

// vweb.v
fn run<T>(port int) {
  mut app := T{}
  ...
}

All that's needed to set up a web application object:

fn main() {
  vweb.run<App>(port)
}

Now it has to be done with

fn main() {
  mut app := App{}
  vweb.run(mut app, port)
}

Which syntax do you prefer?

medvednikov avatar Oct 21 '19 12:10 medvednikov

The second syntax is easier to understand for me:

fn main() {
  mut app := App{}
  vweb.run(mut app, port)
}

spytheman avatar Oct 21 '19 12:10 spytheman

Second syntax for me as well

Delta456 avatar Oct 21 '19 12:10 Delta456

I agree with @spytheman and @Delta456 that the second syntax is more intuitive.

Bowero avatar Oct 21 '19 12:10 Bowero

I think this is not such a big problem. Second variant is much better for me. It's fine to call generic functions without the <T>, I think it's cleaner and it's usual in most languages suporting generics. Being strict is fine, but there are places where it makes sence to give some freedom.

But I have another remark. What about generic struct? Can we do that now? Is it needed?

struct Things<T> {
    a T
    b T
    c int
}

small := Things<int>{1, 1, 2}
big := Things<i64>{1, 1, 2}

// Or should it also then be?
small := Things{1, 1, 2}
big := Things{i64(1), i64(1), 2}

Like containers of different types in C++. Perhaps that comes from C++ to me, but I have a feeling that you have to indicate the type of the element when you create a general container, but you never bother specifying the exact type when calling the function (the type is inferred from the parameters). And I like it this way.

avitkauskas avatar Oct 21 '19 13:10 avitkauskas

@avitkauskas Generic structs are not yet implemented, as you have probably found out by now.

For generic structs, it would be necessary to specify the type to allow partial initialization.

shsr04 avatar Oct 21 '19 18:10 shsr04

I think if it's a case of either/or, then the first syntax is a lot better. However, I do like the old way of calling vweb.run()... Could we make it optional? Best of both worlds?

tobyjwebb avatar Oct 22 '19 05:10 tobyjwebb

The first syntax is the old syntax :) @tobyjwebb

medvednikov avatar Oct 22 '19 05:10 medvednikov

:man_facepalming: Sorry, I meant the other way round... Not enough coffee yet.

tobyjwebb avatar Oct 22 '19 05:10 tobyjwebb

@avitkauskas generic structs will be certainly supported. And yes, no way around specifying explicit types:

struct List<T>{}

list := List<int>{}

medvednikov avatar Oct 22 '19 05:10 medvednikov

This issue was opened primarily to discuss generic function call syntax. But we also have to discuss the generics type restriction syntax.

Today this compiles and crashes at run time yet:

fn div<T>(a, b T) T {
    return a / b
}
fn main() {
    div('aaa', 'bbb')
}

So, we need a syntax to restrict generic types to only certain sets of types, so that compiler could check that and though an error during compilation. Could it be one of these?

fn div<T {int, u32, i64, u64}>(a, b T) T { ... }
fn div<T [i64, u64], U [int]>(a T, b U) T { ... }

or perhaps even better

typeset Numbers {i8, byte, i16, u16, int, u32, i64, u64}
typeset UnsignedNumbers {byte, u16, u32, u64}
// with most usefull typesets predefined somewhere?
fn div<T Numbers, U UnsignedNumbers>(a T, b U) T { ... }

Or was it already thought about and decided?

avitkauskas avatar Oct 22 '19 08:10 avitkauskas

It doesn't compile for me, I get a C error.

We just need to make V handle it:

error: invalid operands to binary expression ('string' (aka 'struct string') and 'string')
return  a / b ;

I think it's fine to just check for every type during compilation without complicating the language with concepts for now.

medvednikov avatar Oct 22 '19 09:10 medvednikov

Today this compiles and crashes at run time yet:

I would treat it as a bug, and try to apply compile time checking on the generic function, and not modify the syntax.

tobyjwebb avatar Oct 22 '19 09:10 tobyjwebb

PS: Regarding what I mentioned earlier, about the <> being required or not:

I would make them not required if you can infer the type from the arguments to the function, otherwise they would need to be explicitly defined.

fn foo<T>(f T) { ... }
fn bar<T>(x int) { /* do something with T. NOTE: T not used in parameters */ }
// <> not required to call foo:
foo('hello')
// <> required to call bar:
bar<App>(80)

tobyjwebb avatar Oct 22 '19 09:10 tobyjwebb

Oh yes, it really gives C error on compilation, not on run time. My bad. It's OK if it would be checked for every type during the compilation. Though it would restrict us to write generic functions that ONLY work for all types, we could not write then generic functions working only on numbers, or only on signed numbers etc.

avitkauskas avatar Oct 22 '19 09:10 avitkauskas

I'm not a big proponent of generics until it can be prevented that incompatible types (for the function operations) can be clearly sorted out (I think to ensure this function overloading would be necessary) e.g. adding two integers would call another function then adding (concatenating) two strings or adding two arrays of integers or adding two arrays of strings or... you get my point.

For example what happens if I provide a struct as T arg here:

fn devide<T>(a T, b T) T {
    return a/b
}

General question: are generics so beneficial at all that the clearity of syntax can be sacrificed for this matter? Probably V doesn't need its own version of any syntax construct of all other languages out there.

However this could be helpful: Generic Programming

Bunus reading: Advantage of Java Generics it seems like all 3 points mentioned there solve Java problems V does not have, or am I wrong?

gslicer avatar Oct 22 '19 12:10 gslicer

For example what happens if I provide a struct as T arg here:

Compilation error. / cannot be used with this type.

I'm not a fan of generics either, but sometimes they are necessary.

The goal is to keep them small and simple.

medvednikov avatar Oct 23 '19 08:10 medvednikov

As long V detects such conflicts at compile-time it's fine for me, I would prefer the C++ like syntax having those where T could be any single upper-case letter and in case of multiple disjunct types <T,U> the compiler would actually also check: assert T.type() != U.type()

gslicer avatar Oct 23 '19 09:10 gslicer

That's a good idea.

Not sure about allowing all letters yet. I like a limited set.

People can get really create with generics, I'd like to limit their usage a bit to achieve simpler code.

medvednikov avatar Oct 23 '19 09:10 medvednikov

Why have assert T != U?

What if the user sometimes wants to pass the same type?

El mié., 23 oct. 2019 11:11, Alexander Medvednikov [email protected] escribió:

That's a good idea.

Not sure about allowing all letters yet. I like a limited set.

People can get really create with generics, I'd like to limit their usage a bit to achieve simpler code.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/vlang/v/issues/2469?email_source=notifications&email_token=AAE4HSNF6BJ6T336JY77SUDQQAIMDA5CNFSM4JC4T3T2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECAVVRQ#issuecomment-545348294, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE4HSIGDBGUMAACM5A24MTQQAIMDANCNFSM4JC4T3TQ .

tobyjwebb avatar Oct 23 '19 09:10 tobyjwebb

@tobyjwebb I think this is to improve clarity and readability - when the template creator designed the template to work with same types he should use the same letter for both arguments, don't you think?

gslicer avatar Oct 23 '19 09:10 gslicer