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

Allow for Named and Default Generic Type Paramters #610

Open
robkuz opened this Issue Sep 26, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@robkuz

robkuz commented Sep 26, 2017

When using types with lots of generic type parameters ( > 2) instantiating these types becomes pretty
cumbersome and one easily looses track which parameter belongs where. So I suggest we allow to have named parameters on type instantiations too.

[<NamedTypeParams>]
type SomeRecord<'foo, 'bar, 'baz, 'foobar> =
{
    foo: 'foo
    bar: 'bar
    baz: 'baz
    foobar: 'foobar
}

type ConcreteRecord =
    SomeRecord<
        'bar = int
        'baz = float
        'foo = string
        'foobar = Option<int>>

Also it would be nice if we could support default type parameters

[<NamedTypeParams>]
type SomeRecord<'foo, 'bar, 'baz = Option<int>, 'foobar> =
{
    foo: 'foo
    bar: 'bar
    baz: 'baz
    foobar: 'foobar
}

type ConcreteRecord =
    SomeRecord<
        'foo = string
        'bar = int
        'foobar = Option<string>>

The existing way of approaching this problem in F# is not to have types with to many generic type parameters

Pros and Cons

The advantages of making this adjustment to F# are mostly "documentary" in nature and the fact that one is not bound to a strict order

Extra information

Estimated cost (XS, S, M, L, XL, XXL): M

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@Rickasaurus

This comment has been minimized.

Show comment
Hide comment
@Rickasaurus

Rickasaurus Nov 8, 2017

This is a pretty neat idea, I'm not sure where I'd use it though. What about named parameters like with methods? e.g. type MyType<'foo=Foo, 'bar=Bar>

Rickasaurus commented Nov 8, 2017

This is a pretty neat idea, I'm not sure where I'd use it though. What about named parameters like with methods? e.g. type MyType<'foo=Foo, 'bar=Bar>

@dsyme

This comment has been minimized.

Show comment
Hide comment
@dsyme

dsyme Nov 16, 2017

Collaborator

@robkuz Would the defaults flow through inference, so

let f (a,b,c,d) = { foo=a; bar=b; baz=c;  foobar=d }

somehow gets default inference behaviour?

Collaborator

dsyme commented Nov 16, 2017

@robkuz Would the defaults flow through inference, so

let f (a,b,c,d) = { foo=a; bar=b; baz=c;  foobar=d }

somehow gets default inference behaviour?

@robkuz

This comment has been minimized.

Show comment
Hide comment
@robkuz

robkuz Nov 16, 2017

I am not sure I understand your question.

in the case of an record construction the behaviour should be pretty clear. namely it overrides the defaults.
Maybe a better example would be this

let f (a:'a ,b: 'b ,c: 'c, d' 'd) : SomeRecord<'a, 'b, 'd> = { foo=a; bar=b; baz=c;  foobar=d }

now the question would be is the return type now defined as

SomeRecord<'a, 'b, 'c, 'd>

or is it

SomeRecord<'a, 'b, Option<int>, 'd>

and the function definition would consequently (and implicitly) be changed to

let f (a:'a ,b: 'b ,c: Option<int>, d' 'd) : SomeRecord<'a, 'b, Option<int>, 'd> = 
     { foo=a; bar=b; baz=c;  foobar=d }

So which takes precedence?

I would strongly opt for the first option. That is definitions on the value level take precedence. And hence I would even not allow the definition on the value level but purely on the type level

robkuz commented Nov 16, 2017

I am not sure I understand your question.

in the case of an record construction the behaviour should be pretty clear. namely it overrides the defaults.
Maybe a better example would be this

let f (a:'a ,b: 'b ,c: 'c, d' 'd) : SomeRecord<'a, 'b, 'd> = { foo=a; bar=b; baz=c;  foobar=d }

now the question would be is the return type now defined as

SomeRecord<'a, 'b, 'c, 'd>

or is it

SomeRecord<'a, 'b, Option<int>, 'd>

and the function definition would consequently (and implicitly) be changed to

let f (a:'a ,b: 'b ,c: Option<int>, d' 'd) : SomeRecord<'a, 'b, Option<int>, 'd> = 
     { foo=a; bar=b; baz=c;  foobar=d }

So which takes precedence?

I would strongly opt for the first option. That is definitions on the value level take precedence. And hence I would even not allow the definition on the value level but purely on the type level

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment