类型属性的自动返回值

huangapple go评论70阅读模式
英文:

Automatic returning of value of a property of a type

问题

在我的新工作中,他们采用了一种范例,其中有数千个对象的属性是某种类型,比如Long。但这被称为LongField。要获取该字段的实际值,我必须使用property.AsLong。我希望只需键入property,而不必添加.AsLong、.AsInteger或.AsBoolean等。

有没有办法编辑基类(们),让我只需使用property,而不是property.AsWhatever?

编辑:例如,以下是我所指的代码。

Dim asdf as Long = myThing.thingsID

这显示了一个错误:“无法将类型为'LongField'的值转换为'Long'”。

我宁愿不必记住我是否在处理Long还是LongField,然后不得不为所有东西添加.AsLong。对于整数、布尔值、日期等也是一样的。

英文:

In my new job, they have a paradigm where they have thousands of properties of objects that are a type of, say, Long. But this is called a LongField. And to get the actual value of that field, I have to use property.AsLong. I'd love to just type property and not have to add the .AsLong or .AsInteger or .AsBoolean, etc.

Is there any way that I can edit the base class(es) to allow me to just use property and not property.AsWhatever?

EDIT: for example code, here is what I mean.

Dim asdf as Long = myThing.thingsID

That shows an error: "Value of type 'LongField' cannot be converted to 'Long'"

I'd prefer to not have to remember if I'm dealing with a Long or a LongField, and then have to add .AsLong to everything. Same with ints, bools, dates, etc.

答案1

得分: 1

以下是您要翻译的内容:

如果您有这样的类型:

Public Class LongField

    Private value As Long

    Public Sub New(value As Long)
        Me.value = value
    End Sub

    Public Function ToLong() As Long
        Return value
    End Function

End Class

那么以下代码的最后两行是不允许的,即使使用 Option Strict Off 也不行:

Dim l As Long = 123
Dim lf As New LongField(456)

l = lf
lf = l

但如果您将以下代码添加到该类型中:

Public Shared Widening Operator CType(source As LongField) As Long
    Return source.value
End Operator

Public Shared Widening Operator CType(source As Long) As LongField
    Return New LongField(source)
End Operator

那么这两行都会变得合法,并且将执行隐式转换,即使使用 Option Strict On 也是如此。

这将允许任何现有代码继续按原样工作,并允许将 LongField 对象视为 Long,至少用于获取和设置。但它仍然是一个类,因此受到引用类型语义的影响,而不是值类型语义。很难说这是否会破坏此类型的原始目的,因为很难确切知道该目的是什么。

值得注意的是:

someObject.SomeLongFieldProperty = someLong

将导致该属性引用类型为 LongField 的新对象。也许,根据当前的实现方式,实际上应该更改同一个 LongField 对象中的 Long 值。从您提供的信息中无法确定这是否是预期的情况。不过,如果确实如此,那么除非该属性是 ReadOnly,否则无法保证。

英文:

If you have a type like this:

Public Class LongField

    Private value As Long

    Public Sub New(value As Long)
        Me.value = value
    End Sub

    Public Function ToLong() As Long
        Return value
    End Function

End Class

then the last two lines of the following code are not allowed, even with Option Strict Off:

Dim l As Long = 123
Dim lf As New LongField(456)

l = lf
lf = l

If you add the following code to that type though:

Public Shared Widening Operator CType(source As LongField) As Long
    Return source.value
End Operator

Public Shared Widening Operator CType(source As Long) As LongField
    Return New LongField(source)
End Operator

both those lines become legal and will perform implicit conversions, even with Option Strict On.

This would allow any existing code to continue to work as is and for that ToLong method to continue to be used in new code, while also allowing a LongField object to be treated as though it were a Long, for the purposes of getting and setting at least. It is still a class though, so is subject to reference type semantics rather than value type semantics. It's hard to say whether this would defeat the original purpose of such a type because it's hard to know what that purpose would have been exactly.

It's worth noting that this:

someObject.SomeLongFieldProperty = someLong

would result in that property referring to a new object of type LongField. It may be that, the way things are currently implemented, you're actually supposed to change the Long value within the same LongField object. We can't tell from what you've provided. That said, if that was supposed to be the case then it couldn't have been guaranteed unless the property was ReadOnly anyway.

huangapple
  • 本文由 发表于 2023年6月15日 06:07:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定