dotnet / dotnet/vblang

Custom implicit Ctype is not used in dynamic mode

Open
#613 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

VB can't call the implicit ctype operator on dynamic objects. This code gives the runtime error: Can't convert from Test To String:
```vb
Option Strict Off
Option Explicit On

Module Program
Sub Main()
Dim o As Object = New Test
Dim s As String = o
End Sub
End Module

Class Test
Public Shared Widening Operator CType(t As Test) As String
Return t.ToString()
End Operator
End Class
```

While the same code runs correctly in C#:
```c#
dynamic o = new Test();
string s = o;

class Test
{
public static implicit operator string (Test t) => t.ToString();
}
```

I think Vb should adapt the [C# spcs about this](https://github.com/dotnet/csharplang/blob/f63f70960c8c7d162eb645ad7ff0c891797995f9/spec/conversions.md#implicit-dynamic-conversions), after all vb was a dynamic langish since before .NET, where even declaring variables was not mandatort (Option Explicit Off was the default in old vb versions before dotnet.)
Also, I suggest that dynamically or not, vb should use .ToString to implicitly convert any type to a string as lomg as ther is no a suitable CType defined to do that. This is more like how VB does things.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.