Skip to content

Documentation contains invalid type extension for seq<'T> which is a type abbreviation #1670

Closed

Description

@abelbraaksma

The documentation about type extensions contains the following example code:

type seq<’T> with
    /// Repeat each element of the sequence n times
    member xs.RepeatElements(n: int) =
        seq { for x in xs do for i in 1 .. n do yield x }

This example code won't compile, it will throw:

error FS0964: Type abbreviations cannot have augmentations

and

error FS0895: Type abbreviations cannot have members

The proper code would be (assuming System.Collections.Generic is opened)

type IEnumerable<'T> with
    /// Repeat each element of the sequence n times
    member xs.RepeatElements(n: int) =
        seq { for x in xs do for i in 1 .. n do yield x }

Note that the text itself mentions that type abbreviations cannot be extended.

Activity

  1. dsyme commented on Oct 27, 2016

    @dsyme
    Contributor

    @abelbraaksma Please fix by making a PR to the docs directly?

  2. dsyme commented on Oct 27, 2016

    @dsyme
    Contributor
  3. abelbraaksma commented on Oct 27, 2016

    @abelbraaksma
    ContributorAuthor

    @dsyme, yes, I'll look into that (never worked with github sources before). I'm wondering though if my suggested change is a good one. Having type IEnumerable<'T> with... does not append the new method to Seq, because Seq is a module and modules cannot be extended (unfortunately so, but that's a different discussion).

    Creating a type Seq = ... would do what you'd expect, but that is not a type extension.

    What the code does is (of course) creating an instance member extension for sequences, but that's hardly idiomatic. Perhaps the whole suggestion should go and a different extension that's more fsharpish should be used instead? Any ideas?

  4. added a commit that references this issue on Oct 27, 2016
  5. added a commit that references this issue on Dec 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions