{"action":"created","issue":{"url":"https://api.github.com/repos/vlang/v/issues/2469","repository_url":"https://api.github.com/repos/vlang/v","labels_url":"https://api.github.com/repos/vlang/v/issues/2469/labels{/name}","comments_url":"https://api.github.com/repos/vlang/v/issues/2469/comments","events_url":"https://api.github.com/repos/vlang/v/issues/2469/events","html_url":"https://github.com/vlang/v/issues/2469","id":509931849,"node_id":"MDU6SXNzdWU1MDk5MzE4NDk=","number":2469,"title":"Generics syntax discussion","user":{"login":"medvednikov","id":687996,"node_id":"MDQ6VXNlcjY4Nzk5Ng==","avatar_url":"https://avatars2.githubusercontent.com/u/687996?v=4","gravatar_id":"","url":"https://api.github.com/users/medvednikov","html_url":"https://github.com/medvednikov","followers_url":"https://api.github.com/users/medvednikov/followers","following_url":"https://api.github.com/users/medvednikov/following{/other_user}","gists_url":"https://api.github.com/users/medvednikov/gists{/gist_id}","starred_url":"https://api.github.com/users/medvednikov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medvednikov/subscriptions","organizations_url":"https://api.github.com/users/medvednikov/orgs","repos_url":"https://api.github.com/users/medvednikov/repos","events_url":"https://api.github.com/users/medvednikov/events{/privacy}","received_events_url":"https://api.github.com/users/medvednikov/received_events","type":"User","site_admin":false},"labels":[{"id":1420603774,"node_id":"MDU6TGFiZWwxNDIwNjAzNzc0","url":"https://api.github.com/repos/vlang/v/labels/discussion","name":"discussion","color":"4f69ea","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":15,"created_at":"2019-10-21T12:31:21Z","updated_at":"2019-10-23T03:01:10Z","closed_at":null,"author_association":"MEMBER","body":"Generics are back thanks to @shsr04.\r\n\r\nThis time they are more powerful (for example, multiple types are supported).\r\n\r\nYou can see some examples here: https://github.com/vlang/v/blob/master/vlib/compiler/tests/generic_test.v\r\n\r\nOne big change is the type parameters being removed from the function call:\r\n\r\n```v\r\nfoo<int>(1)\r\nfoo<string>('str')\r\nfoo<User>(user)\r\n```\r\n\r\nbecomes\r\n\r\n```v\r\nfoo(1)\r\nfoo('str')\r\nfoo(user)\r\n```\r\n\r\nTo 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.\r\n\r\nAlso some functionality is lost. vweb uses the following construct to build an application object:\r\n\r\n```v\r\n// vweb.v\r\nfn run<T>(port int) {\r\n  mut app := T{}\r\n  ...\r\n}\r\n```\r\n\r\nAll that's needed to set up a web application object:\r\n\r\n```v\r\nfn main() {\r\n  vweb.run<App>(port)\r\n}\r\n```\r\n\r\nNow it has to be done with \r\n\r\n```v\r\nfn main() {\r\n  mut app := App{}\r\n  vweb.run(mut app, port)\r\n}\r\n```\r\n\r\nWhich syntax do you prefer?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/vlang/v/issues/comments/545243045","html_url":"https://github.com/vlang/v/issues/2469#issuecomment-545243045","issue_url":"https://api.github.com/repos/vlang/v/issues/2469","id":545243045,"node_id":"MDEyOklzc3VlQ29tbWVudDU0NTI0MzA0NQ==","user":{"login":"Delta456","id":28479139,"node_id":"MDQ6VXNlcjI4NDc5MTM5","avatar_url":"https://avatars1.githubusercontent.com/u/28479139?v=4","gravatar_id":"","url":"https://api.github.com/users/Delta456","html_url":"https://github.com/Delta456","followers_url":"https://api.github.com/users/Delta456/followers","following_url":"https://api.github.com/users/Delta456/following{/other_user}","gists_url":"https://api.github.com/users/Delta456/gists{/gist_id}","starred_url":"https://api.github.com/users/Delta456/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Delta456/subscriptions","organizations_url":"https://api.github.com/users/Delta456/orgs","repos_url":"https://api.github.com/users/Delta456/repos","events_url":"https://api.github.com/users/Delta456/events{/privacy}","received_events_url":"https://api.github.com/users/Delta456/received_events","type":"User","site_admin":false},"created_at":"2019-10-23T03:01:10Z","updated_at":"2019-10-23T03:01:10Z","author_association":"MEMBER","body":"> 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.\r\n> \r\n> For example what happens if I provide a struct as T arg here:\r\n> \r\n> ```v\r\n> fn devide<T>(a T, b T) T {\r\n>     return a/b\r\n> }\r\n> ```\r\n> \r\n> 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.\r\n> \r\n> However this could be helpful: [Generic Programming](https://en.wikipedia.org/wiki/Generic_programming)\r\n> \r\n> Bunus reading: [Advantage of Java Generics](https://www.javatpoint.com/generics-in-java) it seems like all 3 points mentioned there solve Java problems V does not have, or am I wrong?\r\n\r\nFor this the custom `struct` must have a operator overloaded `/` to make it work."}}
{"actor":{"display_login":"Delta456"}}