1 |
#Region "Microsoft.VisualBasic::71ee759ed63d841ed4e808bcd1d165c7, Microsoft.VisualBasic.Core\Language\API.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
#End Region
|
44 |
|
45 |
Imports System.Runtime.CompilerServices
|
46 |
Imports Microsoft.VisualBasic.Language.Default
|
47 |
Imports Microsoft.VisualBasic.Language.Perl
|
48 |
Imports Microsoft.VisualBasic.Language.Vectorization
|
49 |
Imports Microsoft.VisualBasic.Scripting.Runtime
|
50 |
|
51 |
Namespace Language
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
Public Module LanguageAPI
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
Friend ReadOnly defaultAssert As New DefaultValue(Of Assert(Of Object)) With {
|
62 |
.Value = AddressOf ExceptionHandle.Default,
|
63 |
.assert = Function(assert)
|
64 |
Return assert Is Nothing
|
65 |
End Function
|
66 |
}
|
67 |
|
68 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
69 |
Public Function TypeDef(Of T)() As TypeSchema
|
70 |
Return New TypeSchema(GetType(T))
|
71 |
End Function
|
72 |
|
73 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
74 |
<Extension>
|
75 |
Public Function TypeInfo(Of T)(x As T) As TypeSchema
|
76 |
Return TypeDef(Of T)()
|
77 |
End Function
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
85 |
Public Function Empty(Of T)() As DefaultValue(Of T())
|
86 |
Return {}
|
87 |
End Function
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
模拟R语言之中的``%||%``操作符
|
93 |
|
94 |
```R
|
95 |
`%||%` <- function(x, y) if (is.null(x)) y else x
|
96 |
|
97 |
NULL %||% 123
|
98 |
# 123
|
99 |
|
100 |
233 %||% 123
|
101 |
# 233
|
102 |
```
|
103 |
|
104 |
<typeparam name="T"></typeparam>
|
105 |
<param name="x"></param>
|
106 |
<param name="isNothing"></param>
|
107 |
<returns></returns>
|
108 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
109 |
Public Function [Default](Of T)(x As T, Optional isNothing As Assert(Of Object) = Nothing) As DefaultValue(Of T)
|
110 |
Return New DefaultValue(Of T) With {
|
111 |
.Value = x,
|
112 |
.assert = isNothing Or defaultAssert
|
113 |
}
|
114 |
End Function
|
115 |
|
116 |
|
117 |
Using this value as the default value for this <typeparamref name="T"/> type.
|
118 |
|
119 |
<typeparam name="T"></typeparam>
|
120 |
<param name="x"></param>
|
121 |
<param name="[If]"></param>
|
122 |
<returns></returns>
|
123 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
124 |
<Extension>
|
125 |
Public Function AsDefault(Of T)(x As T, Optional [If] As Assert(Of Object) = Nothing) As DefaultValue(Of T)
|
126 |
Return [Default](x, [If])
|
127 |
End Function
|
128 |
|
129 |
Public Function DefaultValue(Of T)(value As T) As DefaultValue(Of T)
|
130 |
Return New DefaultValue(Of T) With {
|
131 |
.Value = value,
|
132 |
.assert = defaultAssert
|
133 |
}
|
134 |
End Function
|
135 |
|
136 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
137 |
<Extension>
|
138 |
Public Function [When](Of T)([default] As T, expression As Boolean) As DefaultValue(Of T)
|
139 |
Return [default].AsDefault().When(expression)
|
140 |
End Function
|
141 |
|
142 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
143 |
<Extension>
|
144 |
Public Function [When](Of T)([default] As T, expression As Assert(Of T)) As DefaultValue(Of T)
|
145 |
Return [default].AsDefault().When(assert:=expression)
|
146 |
End Function
|
147 |
|
148 |
|
149 |
Helper for update the value property of <see cref="Value(Of T)"/>
|
150 |
|
151 |
```vbnet
|
152 |
Call Let$(<see cref="Value(Of T)"/> = x)
|
153 |
```
|
154 |
|
155 |
<typeparam name="T"></typeparam>
|
156 |
<param name="value"></param>
|
157 |
<returns></returns>
|
158 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
159 |
Public Function Let$(Of T)(value As T)
|
160 |
Try
|
161 |
Return CStrSafe(value)
|
162 |
Catch ex As Exception
|
163 |
|
164 |
Return Nothing
|
165 |
End Try
|
166 |
End Function
|
167 |
|
168 |
#Region "Helper for ``With``"
|
169 |
|
170 |
|
171 |
My self: <see cref="LanguageAPI.ByRef(Of T)(T)"/>
|
172 |
|
173 |
<typeparam name="T"></typeparam>
|
174 |
<returns></returns>
|
175 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
176 |
Public Function Self(Of T)() As Func(Of T, T)
|
177 |
Return AddressOf [ByRef]
|
178 |
End Function
|
179 |
|
180 |
|
181 |
Extension method for VisualBasic ``With`` anonymous variable syntax source reference helper
|
182 |
|
183 |
<typeparam name="T"></typeparam>
|
184 |
<param name="x"></param>
|
185 |
<returns></returns>
|
186 |
|
187 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
188 |
<Extension> Public Function [ByRef](Of T)(x As T) As T
|
189 |
Return x
|
190 |
End Function
|
191 |
|
192 |
|
193 |
Extension method for VisualBasic ``With`` anonymous variable syntax for determine that source reference is nothing or not?
|
194 |
|
195 |
<typeparam name="T"></typeparam>
|
196 |
<param name="x"></param>
|
197 |
<returns></returns>
|
198 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
199 |
<Extension>
|
200 |
Public Function IsNothing(Of T As Class)(x As T) As Boolean
|
201 |
Return x Is Nothing
|
202 |
End Function
|
203 |
#End Region
|
204 |
|
205 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
206 |
<Extension>
|
207 |
Public Function AsVector(strings As IEnumerable(Of String)) As StringVector
|
208 |
Return New StringVector(strings)
|
209 |
End Function
|
210 |
|
211 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
212 |
Public Function list(ParamArray args As ArgumentReference()) As Dictionary(Of String, Object)
|
213 |
Return args.ToDictionary(Function(a) a.name, Function(a) a.value)
|
214 |
End Function
|
215 |
|
216 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
217 |
<Extension>
|
218 |
Public Function AsNumeric(list As Dictionary(Of String, Object)) As Dictionary(Of String, Double)
|
219 |
Return list.ToDictionary(Function(t) t.Key, Function(t) CDbl(t.Value))
|
220 |
End Function
|
221 |
|
222 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
223 |
<Extension>
|
224 |
Public Function AsString(list As Dictionary(Of String, Object)) As Dictionary(Of String, String)
|
225 |
Return list.ToDictionary(Function(t) t.Key, Function(t) Scripting.ToString(t.Value))
|
226 |
End Function
|
227 |
End Module
|
228 |
End Namespace
|