Skip to content

Commit 485140a

Browse files
committed
Implement type param to chan type conversion.
1 parent b66fa57 commit 485140a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

compiler/prelude/types.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
475475
typ.convertFrom = (src) => $convertToMap(src, typ);
476476
break;
477477
case $kindChan:
478+
typ.convertFrom = (src) => $convertToChan(src, typ);
479+
break;
478480
case $kindFunc:
479481
break;
480482
default:
@@ -1202,4 +1204,14 @@ const $convertToArray = (src, dstType) => {
12021204
*/
12031205
const $convertToMap = (src, dstType) => {
12041206
return src.$val;
1207+
};
1208+
1209+
/**
1210+
* Convert to chan types.
1211+
*
1212+
* dstType.kind must be $kindChan. Src must be a wrapped chan value. Returned
1213+
* value will always be a bare $Chan object representing the channel.
1214+
*/
1215+
const $convertToChan = (src, dstType) => {
1216+
return src.$val;
12051217
};

tests/typeparams/conversion_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type ( // Named types for use in conversion test cases.
5757
arr [3]byte
5858
arrPtr *[3]byte
5959
m map[string]string
60+
ch chan string
6061
)
6162

6263
type numeric interface {
@@ -267,10 +268,20 @@ func (tc mapConversion[srcType, dstType]) Run(t *testing.T) {
267268
checkConversion(t, tc.src, dstType(tc.src), tc.want)
268269
}
269270

271+
type chanConversion[srcType ~chan string, dstType ~chan string] struct {
272+
src srcType
273+
want dstType
274+
}
275+
276+
func (tc chanConversion[srcType, dstType]) Run(t *testing.T) {
277+
checkConversion(t, tc.src, dstType(tc.src), tc.want)
278+
}
279+
270280
func TestConversion(t *testing.T) {
271281
strVar := "abc"
272282
stVar := st{s: "abc", i: 42}
273283
arrVal := [3]byte{1, 2, 3}
284+
chanVal := make(chan string)
274285

275286
tests := []conversionTest{
276287
// $convertToInt64
@@ -372,6 +383,9 @@ func TestConversion(t *testing.T) {
372383
// $convertToMap
373384
mapConversion[map[string]string, m]{src: map[string]string{"abc": "def"}, want: m{"abc": "def"}},
374385
mapConversion[map[string]string, m]{src: nil, want: nil},
386+
// $convertToChan
387+
chanConversion[chan string, ch]{src: chanVal, want: ch(chanVal)},
388+
chanConversion[chan string, ch]{src: nil, want: nil},
375389
}
376390

377391
for _, test := range tests {

0 commit comments

Comments
 (0)