Skip to content

Conversation

@pkulikov
Copy link
Contributor

@pkulikov pkulikov commented Jul 4, 2018

The snippet code runs successfully in try.dot.net; it hasn't before. I assume that means snippets in docs can be interactive again.

@pkulikov pkulikov requested a review from BillWagner as a code owner July 4, 2018 20:04
@rpetrusha
Copy link
Contributor

rpetrusha commented Jul 4, 2018

It would be really good to make these two examples interactive. But I'm still not able to run them successfully from the topic, @pkulikov, although I am able to run them directly in try.dot.net. @LadyNaggaga, do you have any sense of why that might be?

@pkulikov
Copy link
Contributor Author

pkulikov commented Jul 4, 2018

@rpetrusha indeed, that's a good catch. Thank you.

Page under consideration:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in

@LadyNaggaga when I run the following code

Span<int> numbers = new int[] { 3, 14, 15, 92, 6 };
foreach (int number in numbers)
{
    Console.Write($"{number} ");
}
Console.WriteLine();

in the first interactive block of the page (just replaced all the code with the one above), the error messages are the following:

(1,1): error CS8345: Field or auto-implemented property cannot be of type 'Span<int>' unless it is an instance member of a ref struct.
(2,1): error CS8344: foreach statement cannot operate on enumerators of type 'Span<int>.Enumerator' in async or iterator methods because 'Span<int>.Enumerator' is a ref struct.

Should we create another issue to track this?

@pkulikov pkulikov changed the title Make snippets with Span<T> interactive [WIP] Make snippets with Span<T> interactive Jul 4, 2018
@pkulikov
Copy link
Contributor Author

pkulikov commented Jul 4, 2018

@rpetrusha @BillWagner what do you think of the following workaround:

public class Program
{
  public static void Main()
  {
      Span<int> numbers = new int[] { 3, 14, 15, 92, 6 };
      foreach (int number in numbers)
      {
          Console.Write($"{number} ");
      }
  }
}

Such snippet runs interactively in docs. If that's a solution for now, I'll updated the code in the samples repo.

Pros:

  • Examples with Span<T> are interactive

Cons:

  • Too much "noise" of the Program/Main declaration for simple snippets. That also makes such snippets not consistent with others.

@LadyNaggaga if Program/Main are absent, are they generated? And if generated, then Main is async?

@svick
Copy link
Contributor

svick commented Jul 5, 2018

@pkulikov As I understand it, Try.Net by default uses C# scripting. And within scripts, top-level variables are treated as fields, which explains CS8345. Also, you can use await in scripts, which I think means they have to be treated as async methods, which explains CS8344.

I think a good way to handle this would be something similar to what Try.Net already can do when it displays only one #region from a gist: dotnet/try#90 (comment). But I don't know if there is any way to apply that here.

@LadyNaggaga
Copy link
Contributor

As @svick mentioned something similar to this might be a good idea https://try.dot.net/?workspaceType=nodatime.api&fromGist=8e2ea50af097df663d9c8e19eb8fbf0a&bufferId=TryNodaTime.cs@fragment&canShowGithubPanel=true and use #region to specify the code to display

@rpetrusha
Copy link
Contributor

As you suggested, @pkulikov, I think that the best thing to do here is to include the complete code sample rather than just a region of code, Doing something like linking directly to try.dot.net seems to me to create a confusing experience.

Thoughts, @svick, @LadyNaggaga, and @BillWagner?

@pkulikov
Copy link
Contributor Author

pkulikov commented Jul 5, 2018

@svick @LadyNaggaga thank you for the explanation and the example. I've submitted MicrosoftDocs/feedback#489 to track your idea separately.

@pkulikov
Copy link
Contributor Author

pkulikov commented Jul 5, 2018

As for this PR, I'll include the complete Program/Main sample to make the snippet interactive.

@BillWagner
Copy link
Member

I agree with your thoughts @rpetrusha

I think that the best thing to do here is to include the complete code sample rather than just a region of code.

@pkulikov pkulikov changed the title [WIP] Make snippets with Span<T> interactive Make snippets with Span<T> interactive Jul 5, 2018
@pkulikov
Copy link
Contributor Author

pkulikov commented Jul 5, 2018

@BillWagner @rpetrusha as soon as dotnet/samples#162 is merged, this PR is ready to merge as well.

@rpetrusha
Copy link
Contributor

Closing and reopening to begin new build.

@rpetrusha rpetrusha closed this Jul 5, 2018
@rpetrusha rpetrusha reopened this Jul 5, 2018
Copy link
Contributor

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @pkulikov. The code now works in the interactive code runner. I'll merge your PR.

@rpetrusha rpetrusha merged commit fb14a55 into dotnet:master Jul 5, 2018
@pkulikov pkulikov deleted the patch-2 branch July 5, 2018 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants