Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f2b91ba

Browse filesBrowse files
committed
Implement type param to function types conversion support.
1 parent 1bc040e commit f2b91ba
Copy full SHA for f2b91ba

File tree

Expand file treeCollapse file tree

3 files changed

+33
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-2
lines changed

‎compiler/prelude/types.js

Copy file name to clipboardExpand all lines: compiler/prelude/types.js
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
478478
typ.convertFrom = (src) => $convertToChan(src, typ);
479479
break;
480480
case $kindFunc:
481+
typ.convertFrom = (src) => $convertToFunc(src, typ);
481482
break;
482483
default:
483484
$panic(new $String("invalid kind: " + kind));
@@ -1214,4 +1215,14 @@ const $convertToMap = (src, dstType) => {
12141215
*/
12151216
const $convertToChan = (src, dstType) => {
12161217
return src.$val;
1217-
};
1218+
};
1219+
1220+
/**
1221+
* Convert to function types.
1222+
*
1223+
* dstType.kind must be $kindFunc. Src must be a wrapped function value. Returned
1224+
* value will always be a bare JavaScript function.
1225+
*/
1226+
const $convertToFunc = (src, dstType) => {
1227+
return src.$val;
1228+
};

‎tests/gorepo/run.go

Copy file name to clipboardExpand all lines: tests/gorepo/run.go
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ var knownFails = map[string]failReason{
168168
"typeparam/index2.go": {category: generics, desc: "missing index operator support for generic types"},
169169
"typeparam/issue47258.go": {category: generics, desc: "missing operator support for generic types"},
170170
"typeparam/issue47716.go": {category: generics, desc: "unsafe.Sizeof() doesn't work with generic types"},
171-
"typeparam/issue48137.go": {category: generics, desc: "unsupported conversion from func() main.Bar to main.Bar"},
172171
"typeparam/issue48276a.go": {category: generics, desc: "missing support for the comparable type constraint"},
173172
"typeparam/issue48453.go": {category: generics, desc: "make() doesn't support generic slice types"},
174173
"typeparam/issue49295.go": {category: generics, desc: "len() doesn't support generic pointer to array types"},

‎tests/typeparams/conversion_test.go

Copy file name to clipboardExpand all lines: tests/typeparams/conversion_test.go
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type ( // Named types for use in conversion test cases.
5858
arrPtr *[3]byte
5959
m map[string]string
6060
ch chan string
61+
fun func() int
6162
)
6263

6364
type numeric interface {
@@ -277,11 +278,28 @@ func (tc chanConversion[srcType, dstType]) Run(t *testing.T) {
277278
checkConversion(t, tc.src, dstType(tc.src), tc.want)
278279
}
279280

281+
type funcConversion[srcType ~func() int, dstType ~func() int] struct {
282+
src srcType
283+
want dstType
284+
}
285+
286+
func (tc funcConversion[srcType, dstType]) Run(t *testing.T) {
287+
got := dstType(tc.src)
288+
if reflect.TypeOf(got) != reflect.TypeOf(tc.want) {
289+
t.Errorf("Got: %v. Want: converted type is: %v.", reflect.TypeOf(got), reflect.TypeOf(tc.want))
290+
}
291+
292+
if js.InternalObject(got) != js.InternalObject(tc.want) {
293+
t.Errorf("Got: %v != %v. Want: after type conversion function object should remain the same.", got, tc.want)
294+
}
295+
}
296+
280297
func TestConversion(t *testing.T) {
281298
strVar := "abc"
282299
stVar := st{s: "abc", i: 42}
283300
arrVal := [3]byte{1, 2, 3}
284301
chanVal := make(chan string)
302+
funcVal := func() int { return 42 }
285303

286304
tests := []conversionTest{
287305
// $convertToInt64
@@ -386,6 +404,9 @@ func TestConversion(t *testing.T) {
386404
// $convertToChan
387405
chanConversion[chan string, ch]{src: chanVal, want: ch(chanVal)},
388406
chanConversion[chan string, ch]{src: nil, want: nil},
407+
// $convertToFunc
408+
funcConversion[func() int, fun]{src: funcVal, want: fun(funcVal)},
409+
funcConversion[func() int, fun]{src: nil, want: nil},
389410
}
390411

391412
for _, test := range tests {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.