codekingpro/portable-devtools
114k
1// run2 3// Copyright 2022 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7package main8 9func returnOption[T any](n int) Option[T] {10 if n == 1 {11 return Some[T]{}12 } else {13 return None{}14 }15}16 17type Option[T any] interface {18 sealedOption()19}20 21type Some[T any] struct {22 val T23}24 25func (s Some[T]) Value() T {26 return s.val27}28 29func (s Some[T]) sealedOption() {}30 31type None struct{}32 33func (s None) sealedOption() {}34 35func main() {36 s := returnOption[int](1)37 _ = s.(Some[int])38 39 s = returnOption[int](0)40 _ = s.(None)41 42 switch (any)(s).(type) {43 case Some[int]:44 panic("s is a Some[int]")45 case None:46 // ok47 default:48 panic("oops")49 }50}51 