[RFC] Syntactic Dissonance

128 views
Skip to first unread message

John Pritchard

unread,
Jan 6, 2024, 2:53:01 AM1/6/24
to go-nuts
Hi,

Here's a case of "type dissonance" I don't understand.  Why should it be?


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


Tamás Gulácsi

unread,
Jan 6, 2024, 3:21:39 AM1/6/24
to golang-nuts
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

unread,
Jan 6, 2024, 6:55:27 AM1/6/24
to go-nuts
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
 

--
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...@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.

Brian Candler

unread,
Jan 6, 2024, 7:38:04 AM1/6/24
to golang-nuts

John Pritchard

unread,
Jan 6, 2024, 8:59:42 AM1/6/24
to go-nuts
Hi,

we review type diaspora into possibility space.  Perhaps the assertion that the abstract operand is incapable of supporting the implications of its communication.

had proposed that the abstraction deserving of review is captured by the assignment operation.  The semantics of assignment are ambiguous.

Alternatively, parametric types displace the resolution of semantics from static spacetime to dynamic spacetime.  Likewise, the conception of semantics as dynamical.  That the Sort function semantics are related to the dynamic operand type as well as the static function body. 


a problem solved via parametric type abstraction.

A fact which makes the case in favor of incrementing GOPL type theory.  

Best,

John



Reply all
Reply to author
Forward
0 new messages