Hi,

Thinking about types and their conception, I could avoid the type assertion
boilerplate and rationalize the type membership relationship if this code
compiled.

Best,

John


On Sat, Jan 6, 2024 at 3:21 AM Tamás Gulácsi <tgulacs...@gmail.com> wrote:

> Where does TestObject implement the Comparable interface, esp. the Compare
> method?
> I don't see such in that rep.
> The implemented TestObject.Compare method has different signature: it
> requests a TestObject, not a Comparable interface, as your spec!
> This is only the first error.
>
> The second is that a slice of objects cannot be converted to a slice of
> interface - only by manually copying:
>
> ```
> diff --git a/sort_test.go b/sort_test.go
> index 0874721..c89b3b3 100644
> --- a/sort_test.go
> +++ b/sort_test.go
> @@ -13,10 +13,10 @@ type TestObject string
>
>  type TestList []TestObject
>
> -func (this TestObject) Compare(that TestObject) int {
> +func (this TestObject) Compare(that Comparable) int {
>
>   var bi, bj byte
> - var x, y, z int = 0, len(this), len(that)
> + var x, y, z int = 0, len(this), len(that.(TestObject))
>   var d, c int = 0, 0
>
>   if y == z {
> @@ -34,7 +34,7 @@ func (this TestObject) Compare(that TestObject) int {
>
>   for ; x < c; x++ {
>   bi = this[x]
> - bj = that[x]
> + bj = (that.(TestObject))[x]
>   if bi != bj {
>
>   if bi < bj {
> @@ -58,7 +58,11 @@ func (this TestList) Print() {
>  func TestSort(t *testing.T) {
>   var vector TestList = TestList{TestObject("20231219192613"),
> TestObject("20231221074246"), TestObject("20240102214104"),
> TestObject("20231222063428"), TestObject("20240104112200"),
> TestObject("20231217190339"), TestObject("20231213155157"),
> TestObject("20231219065525"), TestObject("20231231120412"),
> TestObject("20231221152849"), TestObject("20240102073948"),
> TestObject("20240101083455")}
>
> - Sort(vector)
> + objs := make([]Comparable, len(vector))
> + for i := range vector {
> + objs[i] = vector[i]
> + }
> + Sort(objs)
>
>   vector.Print()
>  }
> ```
> John Pritchard a következőt írta (2024. január 6., szombat, 8:53:01 UTC+1):
>
>> Hi,
>>
>> Here's a case of "type dissonance" I don't understand.  Why should it be?
>>
>> https://github.com/syntelos/go-sort
>>
>>
>> An interface type not passing through a static public package function
>> that employs the interface.
>>
>> type Comparable interface {
>>
>>
>> Compare(Comparable) int
>>
>> }
>>
>> func Sort(array []Comparable) ([]Comparable)
>>
>>
>> With go-1.20.12:
>>
>> $ go test
>> # github.com/syntelos/go-sort [github.com/syntelos/go-sort.test]
>> ./sort_test.go:61:7: cannot use vector (variable of type TestList) as
>> []Comparable value in argument to Sort
>> FAIL    github.com/syntelos/go-sort [build failed]
>>
>>
>> Any comments?
>>
>> Best,
>>
>> John
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/ba7239c1-cb52-4f86-9e56-da6ffa721fa5n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/ba7239c1-cb52-4f86-9e56-da6ffa721fa5n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAD6mcO-vyGwUQpuF66S%2B6mYQp5ZW6bV4hA3Gu8ofpk_8pZu4yg%40mail.gmail.com.

Reply via email to