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

[RFC] default type parameter for generics #85

Closed
timotheecour opened this issue Apr 6, 2018 · 11 comments
Closed

[RFC] default type parameter for generics #85

timotheecour opened this issue Apr 6, 2018 · 11 comments
Assignees
Labels

Comments

@timotheecour
Copy link
Member

These should be supported IMO:

with typedesc (EDIT this one now works)

proc foo6(T:typedesc = typedesc[int]): auto = return T.name
echo foo6(int) # (or int.foo6) ok
echo foo6() #ok
echo foo6(string) # Error: type mismatch: got <type int> but expected 'type string'

with generics

proc foo3[T=string](): auto = return T.name
echo foo3() # Error: cannot instantiate: 'T'
echo foo3[]() # Error: type mismatch: got <proc [*missing parameters*](): untyped>

links

mentioned here: nim-lang/Nim#3502 (comment)

I like the idea of default generic parameters, maybe named ones like parse[Dest = int] could be cool too. Is there an RFC for this?

@andreaferretti
Copy link

I think your first example should be

import typetraits

proc foo6(T:typedesc = typedesc[int]): auto = return T.name
echo foo6(int) #ok
echo foo6(string) # Error: type mismatch: got  but expected 'type string'
echo foo6()

@timotheecour
Copy link
Member Author

thanks, updated typos and clarified

@zah zah self-assigned this Apr 10, 2018
zah referenced this issue in nim-lang/Nim Jun 12, 2018
zah referenced this issue in nim-lang/Nim Jun 16, 2018
zah referenced this issue in nim-lang/Nim Jun 16, 2018
zah referenced this issue in nim-lang/Nim Jun 16, 2018
@bluenote10
Copy link

It looks like this is working now on devel. Can be closed?

@andreaferretti
Copy link

@bluenote10 The version with typedesc works. The version with generics does not work currently on devel, but in fact it does not even look like valid nim. @zah ?

@timotheecour
Copy link
Member Author

timotheecour commented Jul 24, 2018

@zah thanks for addressing typedesc default params!

The version with generics does not work currently on devel, but in fact it does not even look like valid nim. @zah ?

well, currently Nim allows this proc foo3[T=string](): auto = return T.name but fails upon instantiation as shown in OP. I don't see why it shouldn't be supported though, it's a standard features in languages that have generics (eg C++, D, ... even typescript); not all though, eg not C#; it's being planned for swift (see https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md)

@timotheecour timotheecour changed the title [RFC] default type parameter for generics and typedesc parameters [RFC] default type parameter for generics parameters Jul 24, 2018
@timotheecour timotheecour changed the title [RFC] default type parameter for generics parameters [RFC] default type parameter for generics Jul 24, 2018
@zah
Copy link
Member

zah commented Aug 5, 2018

Default generic parameters are supported for types. For procs, they might be implemented at some point, but given the current compiler internals that would be a complicated patch, so I don't see this coming soon. More details here:
https://github.com/nim-lang/Nim/issues/7517#issuecomment-380272387

My recommendation is to stick with type (formerly typedesc) params as they have better overloading support.

@dom96
Copy link
Contributor

dom96 commented Aug 5, 2018

Why should this be supported? What is a use case that warrants this feature?

@timotheecour
Copy link
Member Author

timotheecour commented Aug 7, 2018

Why should this be supported? What is a use case that warrants this feature?

myParser[T = DefaultType](a:string)

However, given all the recent improvements to typedesc that happened since this bug was created, I'm in support of the following plan:

  1. Use generic types for generic value params, where the types will be inferred from the call-site
  2. Use typedesc for all params that must be explicitly specified by the user.

After that we can finally start profiting:

Adding parse support in the stdlib is fine once the "typedesc vs generic parameter" fight is settled though.

@timotheecour
Copy link
Member Author

timotheecour commented Aug 16, 2018

/cc @Araq @dom96 @zah ping on this: ok to settle #7517 and go on a closing rampage as suggested above ?

@Clyybber
Copy link

Clyybber commented Aug 16, 2018

I don't think we should rush too much here and close valid issues as wontfix just because fixing them will take longer/might be difficult.

@github-actions
Copy link

This RFC is stale because it has been open for 1095 days with no activity. Contribute a fix or comment on the issue, or it will be closed in 7 days.

@github-actions github-actions bot added the Stale label Jun 17, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants