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

Allow for a shorter deconstruction syntax #781

Open
vbfox opened this issue Sep 11, 2019 · 3 comments

Comments

@vbfox
Copy link

commented Sep 11, 2019

Allow for a shorter deconstruction syntax

I propose we allow for a deconstruction syntax that would be much shorter to declare especially for anonymous records. The original idea was how to declare React component props in a Fable context with anonymous records but it would also be very nice for standard records in a lot of cases.

I present the deconstruction happening here in parameter position but the same should work in other deconstruction usages.

Existing syntax

type Props = { foo: string; bar: string; baz: string }

// Existing, working syntax
let myFunction { foo = foo; bar = bar; baz = baz} =  // Props -> unit
    printfn "%s %s %s" foo bar baz

Proposal

// Simplified version, like #653 but in parameter position
let myFunction { foo; bar; baz } =  // Props -> unit
    printfn "%s %s %s" foo bar baz

// Optionally allow to specify one or more types
let myFunction { foo; bar; baz: string } =  // Props -> unit
    printfn "%s %s %O" foo bar baz

Proposal for anonymous records

// Work with anonymous records
let myFunction {| foo; bar; baz |} = // {| foo: string; bar: string; baz: string |} -> unit
    printfn "%s %s %s" foo bar baz

// Work with anonymous records
let myFunction {| foo; bar; baz: string |} = // {| foo: string; bar: string; baz: string |} -> unit
    printfn "%s %s %O" foo bar baz

Pros and Cons

The advantages of making this adjustment to F# are

  • Promote more usage of types when there is a lot of parameters as combined with #653 it would make them as easy to use as parameters (beside the type creation itself)
  • Unify construction and deconstruction if #653 is done
  • Allowing the syntax for anonymous records would make implementation of javascript APIs where record like arguments are common (Like React) easier.

The disadvantages of making this adjustment to F# are ...

  • Additional complexity in the language for developers, especially for the type specifications as I think the no-type version would be relatively obvious to any F# developer

Extra information

Estimated cost (XS, S, M, L, XL, XXL): M or L I think but not sure

Related suggestions:

Existing implementations of the concept

Javascript/TypeScript already works the same with a similar syntax (Except with less type inference)

interface Props { foo: string; bar: string; baz: boolean }

function test1 ({foo, bar, baz}: Props) {
    console.log(`${foo}, ${bar}, ${baz}`)
}

function test2 (p: Props) {
    let {foo, bar, baz} = p
    console.log(`${foo}, ${bar}, ${baz}`)
}

function test3 ({ foo: xfoo, bar : xbar, baz}: Props) {
    console.log(`${xfoo}, ${xbar}, ${baz}`)
}

function test4 (p: Props) {
    let {foo: xfoo, bar: xbar, baz} = p
    console.log(`${xfoo}, ${xbar}, ${baz}`)
}

Affidavit

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 (Not 100% sure I can't see a breaking change in the parameter position at least)
  • I would be willing to help test this
@robkuz

This comment has been minimized.

Copy link

commented Sep 12, 2019

If you implement this then the reverse should also work

let make foo bar baz = {foo; bar; baz} // string -> string -> string -> Props
@vbfox

This comment has been minimized.

Copy link
Author

commented Sep 13, 2019

@robkuz that's already what #653 woudl allow, even if the sample are all with an anonymous record instead of a named one and show the feature with property getters instead of simply local variables.

But I 100% agree that theses 2 features should work together to provide a simpler unified syntax both in construction and deconstruction position

@cartermp

This comment has been minimized.

Copy link
Member

commented Sep 15, 2019

I think this would have a wonderful symmetry with #653

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.