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

VB -> C#: Return statement doesn't include type conversion #496

Closed
GrahamTheCoder opened this issue Jan 14, 2020 · 2 comments
Closed

VB -> C#: Return statement doesn't include type conversion #496

GrahamTheCoder opened this issue Jan 14, 2020 · 2 comments
Assignees
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@GrahamTheCoder
Copy link
Member

Input code

Public Function Empty() As String
    Return 4
End Function

Erroneous output

public string Empty()
{
    return 4;
}

Expected output

public string Empty()
{
    return Conversions.ToString(4);
}

Details

@GrahamTheCoder GrahamTheCoder added the VB -> C# Specific to VB -> C# conversion label Jan 14, 2020
@GrahamTheCoder GrahamTheCoder self-assigned this Jan 19, 2020
@Saibamen
Copy link
Contributor

Saibamen commented Feb 15, 2020

Why return Conversions.ToString(4); instead of return "4";?

@GrahamTheCoder
Copy link
Member Author

GrahamTheCoder commented Feb 15, 2020

Yep good question. The answer is that in the end I'd like to do what you say. But when something's broken, I try to aim to initially get it working as close to what VB produces in the general case as possible. i.e. If it was Return x + 1, I believe the VB compiler generates Return Conversions.ToString(x+1) under the covers.

Once it fundamentally works, I can iterate and cover special cases like:

  • In this specific case we could use _semanticModel.GetConstantValue to optimize away the need for a conversion. In some similar cases this already happens in fact.
  • I haven't checked whether (x+1).ToString() would do the same for an integer type. If so, I'd also like to move to using that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants