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 attributes after the module keyword #757

Open
Happypig375 opened this issue Jun 20, 2019 · 3 comments

Comments

Projects
None yet
4 participants
@Happypig375
Copy link
Contributor

commented Jun 20, 2019

Allow attributes after the module keyword

Consider:

[<AutoOpen>]
module M =
    [<AbstractClass>]
    type C() = class end
    [<Literal>]
    let X = 3

The Literal attribute can be embedded into the let:

[<AutoOpen>]
module M =
    [<AbstractClass>]
    type C() = class end
    let [<Literal>] X = 3 // ✓

Same for the AbstractClass:

[<AutoOpen>]
module M =
    type [<AbstractClass>] C() = class end // ✓
    let [<Literal>] X = 3

But not the AutoOpen.

module [<AutoOpen>] M = // FS0010: Unexpected start of structured construct in definition. Expected identifier, 'global' or other token.
    type [<AbstractClass>] C() = class end // FS0010: Unexpected keyword 'type' in implementation file
    let [<Literal>] X = 3

The existing way of approaching this problem in F# is leaving the AutoOpen above the module.

Pros and Cons

The advantages of making this adjustment to F# are

  1. Consistency - modules are just specialized types, so placing attributes after the keyword should be allowed as well.
  2. Conciseness - we can utilize the horizontal space to our advantage, thus scrolling less.

The disadvantage of making this adjustment to F# is having more ways to do the same thing. However, precedence has been already made for types and lets.

Extra information

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

Related suggestions:
#33
I would say that this should be allowed for members as well.

// Not using a module to ensure that the module suffix is not added
type MyList() =
    // Fields omitted
    [<CompiledName("Map")>]
    static member map f l =
        // Implementation omitted

Thanks for the suggestion
However I will decline this: what we have works, and the proposed saving is only one line, and the lines become loooong. In balance I don't see a net benefit.

Sure, what we have works, but inconsistent language features are worse than having multiple ways to do the same thing. (Recall: Unification of List, Array, Seq functions in F# 4.0)

Whether the lines become long depends on the length of the name of members and attributes. If they are short enough, they deserve to be combined to utilize more horizontal space.

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.
    (This has sort of been decided for members, but not modules. Module definitions tend to be much shorter than member definitions anyways.)

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

This comment has been minimized.

Copy link

commented Jun 24, 2019

The style guide suggests "place the attribute on its own line" (for [<Literal>]s but this naturally extends to other attributes). Consistency is good here.

@cartermp

This comment has been minimized.

Copy link
Member

commented Jun 26, 2019

@charlesroddie although the style guide does mention that, at the end of the day it's only style. I think having consistency in the language is key here; we want people to adopt that guide not because they have to, but because they agree with its contents.

@dsyme

This comment has been minimized.

Copy link
Collaborator

commented Jul 1, 2019

Allowing attributes on module declarations would be a good addition, marking as approved.

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