Define a primitive data type: Difference between revisions

added vb; moved "omit from" reason from comment to tag arg
(add language: Retro)
(added vb; moved "omit from" reason from comment to tag arg)
Line 1,171:
~&n-=<'sum','difference','product','quotient','remainder'>*~ %QI nat</lang>
This code creates a new library of functions of natural numbers by selecting several of them by name from the standard <code>nat</code> library and putting a wrapper around each one that checks the bounds on the result and throws an exception if necessary. These functions can be used as drop-in replacements for the standard ones.
 
=={{header|Visual Basic}}==
 
VB can't really do primitive data types, but they can be faked with classes.
 
TinyInt.cls:
<lang vb>Private mvarValue As Integer
 
Public Property Let Value(ByVal vData As Integer)
If (vData > 10) Or (vData < 1) Then
Error 380 'Invalid property value; could also use 6, Overflow
Else
mvarValue = vData
End If
End Property
 
Public Property Get Value() As Integer
Value = mvarValue
End Property
 
Private Sub Class_Initialize()
'vb defaults to 0 for numbers; let's change that...
mvarValue = 1
End Sub</lang>
 
Usage (in this case, from a form):
<lang vb>Public x As TinyInt
 
Private Sub Form_Click()
'0-11, to give chance of errors; also not integers, because VB massages data to fit, if possible.
x = Rnd * 12
Me.Print x
End Sub
 
Private Sub Form_Load()
Randomize Timer
Set x = New TinyInt '"Set = New" REQUIRED
End Sub</lang>
 
=={{header|Visual Basic .NET}}==
 
Visual Basic .NET has full support for creating your own primitives, but every operator has to be implemented explicitly. Often developers will only implement the parts they are using and skip the rest.
 
Also note that some operators return a Double instead of a new LimitedInt. This was by choice in order to match the behavior of Integers in VB.
Line 1,322 ⟶ 1,360:
{{omit from|PARI/GP}}
{{omit from|TI-83 BASIC}}
{{omit from|TI-89 BASIC}} <!-- |Does not have user-defined data structures. -->}}
1,150

edits