# Concept:- Pseudo-Inherited Structure?

Page 1 of 1

## 6 Replies - 11529 Views - Last Post: 07 November 2013 - 05:06 PM

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

# Concept:- Pseudo-Inherited Structure?

Posted 01 November 2013 - 02:11 PM

Idea:- Pseudo-Inherit?

OK we traditionally can't inherit from a Structure (struct in C#), but what if the compiler did support a kind of basic inheritance, especially over the basic types. Eg Integer and Double?
This isn't true inheritance as any place you can use and Integer you can not use one of these structure inherited objects.

For Example. If I write the following code (reusing the keyword Inherits.
```Public Structure Miles
Inherits Integer

End Structure

```

The compiler actually creates something like the following.
```<ComponentModel.ImmutableObject(True)>
Public Structure Miles
Implements IComparable(Of Miles)

Private _Value As Integer
Public Default ReadOnly Property Value As Integer
Get
Return _Value
End Get
End Property

Shared Widening Operator CType(ByVal x As Integer) As Miles
Return New Miles With {._Value = x}
End Operator

Shared Operator +(ByVal x As Miles) As Miles
Return New Miles With {._Value = +(x.Value)}
End Operator

Shared Operator -(ByVal x As Miles) As Miles
Return New Miles With {._Value = -(x.Value)}
End Operator

Shared Operator +(ByVal x As Miles, ByVal y As Miles) As Miles
Return New Miles With {._Value = x.Value + y.Value}
End Operator

Shared Operator -(ByVal x As Miles, ByVal y As Miles) As Miles
Return New Miles With {._Value = x.Value - y.Value}
End Operator

Shared Operator =(ByVal x As Miles, ByVal y As Miles) As Boolean
Return x.Value = y.Value
End Operator

Shared Operator <>(ByVal x As Miles, ByVal y As Miles) As Boolean
Return x.Value <> y.Value
End Operator

Shared Operator <=(ByVal x As Miles, ByVal y As Miles) As Boolean
Return x.Value <= y.Value
End Operator

Shared Operator >=(ByVal x As Miles, ByVal y As Miles) As Boolean
Return x.Value >= y.Value
End Operator

Shared Operator <(ByVal x As Miles, ByVal y As Miles) As Boolean
Return x.Value < y.Value
End Operator

Shared Operator >(ByVal x As Miles, ByVal y As Miles) As Boolean
Return x.Value > y.Value
End Operator

Public Function CompareTo(other As Miles) As Integer Implements IComparable(Of Miles).CompareTo
Return Me.Value.CompareTo(other.Value)
End Function

Public Overrides Function ToString() As String
Return String.Format("{0}[Miles]", Me.Value)
End Function

Public Overrides Function Equals(obj As Object) As Boolean
Return Me.Value.Equals(obj)
End Function

Public Overrides Function GetHashCode() As Integer
Return Me.Value.GetHashCode
End Function

End Structure

```

Only "Comparison", Unitary Plus and Minus, Addition and Subtraction and the CType Widening Operators are implemented. Hashcode, Equals and CompareTo are inherited from the "base" object.

The compiler treats it as an Integer and Casts it back the Psuedo-Inherited Type. Except where the Operator is disallowed.

What are your thoughts on this concept?
Good Idea?

Is This A Good Question/Topic? 0

## Replies To: Concept:- Pseudo-Inherited Structure?

### #2 andrewsw

Reputation: 6829
• Posts: 28,311
• Joined: 12-December 12

## Re: Concept:- Pseudo-Inherited Structure?

Posted 01 November 2013 - 03:36 PM

What would be the advantage over, say, Extension methods or managing an Integer field in a Class?

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Concept:- Pseudo-Inherited Structure?

Posted 01 November 2013 - 04:17 PM

It'll simplify writing unit systems and make thing more type-safe as the type has the semantic meaning rather than just value integer and an identifier. Plus you don't have reimplement all the basic operators. +a -a a+b a-b a=b a<>b a<=b a>=b a<b a>b

The runtime treats then as whatever the base is <Symbol(Side.RHS,"˚F")> what symbol to display in .ToString
```<Symbol(Side.RHS,"m")>
Structure Meters
Inherits Double
End Structure

<Symbol(Side.RHS,"s")>
Structure Seconds
Inherits Double
End Structure

<Symbol(Side.RHS,"˚")>
Structure Degrees
Inherits Double
End Structure

<Symbol(Side.RHS,"˚K")>
Structure Kelvins
Inherits Double
End Structure

<Symbol(Side.RHS,"˚C")>
Structure Celsius
Inherit Double
End Class

<Symbol(Side.RHS,"˚F")>
Structure Fahrenheit
Inherits Double
End Structure

```

```Dim m1 As Metres = 100
Dim m2 As Metres = 20.25
Dim res = m1 + m2
'Is not Dim res = m1.Value + m2+Value

```

Note that for example Metres isn't a subtype of Double, it just uses it as "template" for the pseudo-inherited type.

Microsoft have done similar things before eg auto-implemented properties.

This post has been edited by AdamSpeight2008: 01 November 2013 - 04:20 PM

### #4 cfoley

• Cabbage

Reputation: 2392
• Posts: 5,025
• Joined: 11-December 07

## Re: Concept:- Pseudo-Inherited Structure?

Posted 02 November 2013 - 07:39 AM

F# has a very good solution for this with its units implementation.

### #5 lordofduct

• I'm a cheeseburger

Reputation: 2668
• Posts: 4,786
• Joined: 24-September 10

## Re: Concept:- Pseudo-Inherited Structure?

Posted 06 November 2013 - 11:47 AM

Seems interesting. There have been several situations where I've wanted to inherit from a struct (especially a system level one like Double or Integer, or even from Enum), but I always ended up finding a different pattern to get the job done.

I do at the same time find it slightly lacking, but that's just probably due to just rolling the idea around, I don't think AdamSpeight feels this is a perfectly well formed answer, but instead a root idea that he'd like to see grow. And I wouldn't mind seeing such an idea grow.

I've seen the units implementation in F# before, and it seemed really cool.

### #6 dbasnett

Reputation: 124
• Posts: 686
• Joined: 01-October 08

## Re: Concept:- Pseudo-Inherited Structure?

Posted 07 November 2013 - 07:08 AM

I saw some version of this by adam at vbforums. I like the idea.

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Concept:- Pseudo-Inherited Structure?

Posted 07 November 2013 - 05:06 PM

The guys over there (typically) feel liked they are being personally attacked when you question them, or show proof that they are wrong in their basic assumptions.
I provided a code example and an unaltered screenshot of the IDE that following code can be valid.
```If a < b < c Then

```

They were making false assumptions about the types. That the return type is the same in both cases of the operator < has to an Boolean (which it doesn't) and the return type has to be the same.

On the Pseudo-Inherited Structure types, they assumed that I wanted to change the basic functionality of the core types. Like change the bit size of an Integer. That they can be used where the base type can be used.

The idea behind them is like a component blueprint, the compiler (in conspiracy with the IDE) construct the basic parts you'll need.