fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Allow for Named and Default Generic Type Parameters

Open robkuz opened this issue 8 years ago • 9 comments

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:

  • [x] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • [x] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [x] 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:

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

robkuz avatar Sep 26 '17 08:09 robkuz

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 avatar Nov 08 '17 20:11 Rickasaurus

@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?

dsyme avatar Nov 16 '17 14:11 dsyme

@dsyme

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 avatar Nov 16 '17 14:11 robkuz

@cartermp Why reopen this? Its not gonna happen.

robkuz avatar Jun 15 '21 16:06 robkuz

Feel free to unsubscribe if you're not interested.

cartermp avatar Jun 15 '21 16:06 cartermp

@cartermp That was not the question. Whats the point of having that many open suggestions when they have zero chance to be ever implemented?

robkuz avatar Jun 15 '21 16:06 robkuz

Eh, unless marked declined, all suggestions still have a possibility of making it in, provided that someone is willing to make it happen, and the BDFL approves.

Happypig375 avatar Jun 15 '21 16:06 Happypig375

@Happypig375 is correct.

cartermp avatar Jun 15 '21 16:06 cartermp

I actually really like this suggestion in principle. It could fit very nicely with F# inference, and those are the sort of features I tend to like.

However three upvotes and not much technical detail doesn't really establish the use cases, nor the host of technical issues which the feature will be sure to encounter, nor the "slippery slope" the feature might bring along with it, nor the potential for confusion on the part of beginners. It's the sort of thing that might take a week or a month to properly investigate (and even then there will be ramifications we don't think of), or an undergraduate project.

As always any more information on practical scenarios that this really helps with would be great.

dsyme avatar Jun 15 '21 17:06 dsyme