#Go Interface-As-Constraint for Generic

38 messages · Page 1 of 1 (latest)

cosmic moss
#

Hi everyone I've been having a good time learning Go! I hope to learn and contribute here!

I'm looking to derive functions from an interface that I'm using as a constraint for generic types but can't figure out if it's possible to do it in the style that I'm after.

Here's the interface:

type Comparable[T any] interface {
  Lesser(T) bool
  Eq(T) bool
}```

and here's a derived function
```go
func Greater[T Comparable[T]](a, b T) bool {
  return !a.Lesser(b) && !a.Eq(b)
}```

I'm looking to have the derived function be applied something like this though instead:
```go
a.Greater(b)

Does anyone know if this is currently possible? Thanks for any tips 🙂

slender jay
#

a.Greater(b) is not currently possible to define. You'd need to be able to construct a generic method with the type of a as receiver, which the present implementation explicitly disallows.

slender jay
#

You can define Greater as you've written, but that's it.

I'm thinking about why not for the first time. It seems like that would mean the compiler would need to do a lot more work to check if an interface applies to a type, since it would also need to look for methods which it can derive from generic declarations.

cosmic moss
#

Thank you for your response! Do you know if it's possible to then change the function declaration of Lesser and Eq to match the style of Greater?

slender jay
#

No, you're using the method form to satisfy the Comparable interface and you're using the Comparable interface in the signature and implementation of Greater.

I see no way to cut this Gordian knot without more language features.

#

That said...
Fancy type systems are antithetical to Go's design goal of simplicity. I've worked in languages powerful enough to express monads and they're a special kind of hell which makes you feel very smart and accomplished while creating more problems than you solve, IME.

cosmic moss
#

I've really enjoyed Go's simplicity. It's a strong feature to me. I'm coming from Haskell and C++ haha. This is the only thing that I've found that I want so far in Go so that's not a bad thing!

slender jay
#

Hahaha! Yeah I worked in Scala, which is what happened to Java when a Haskell programmer got their hands on it.

cosmic moss
#

Trying to create a handful of interfaces where if a programmer uses one as much is derived as possible.

#

I've used Scala before and I agree to me it was hell. When I use Haskell I barely use extensions and try to keep things simple. It's much better than Scala to me

slender jay
#

So. Funny story. I've tried starting out with a lot of type-oriented or object-oriented design when writing go programs.
I'm still not sure the results are better than just coding first and designing only as necessary.

#

Like. You ought to be design interfaces between programs up front.

#

I'm not sure designing types up front within a program in Go has served me well.

cosmic moss
#

by types do you mean structs?

#

and I guess interfaces for constraints

slender jay
#

Interfaces actually.

cosmic moss
#

Yeah right now my plan for interfaces is only for type constraints

slender jay
#

I think there's a proverb. Interfaces in Go should be discovered.

#

Not crafted.

#

Which implies an order of operations for the programmer. 🙂

cosmic moss
#

My first larger Go program will be rewriting my PE and ELF parser. I'm just writing data structures right now to learn

slender jay
#

Ooooo

#

Okay for the purpose of a parser you should read the ast and parser packages in the stdlib

#

There are techniques.

#

They're not bad. The design they have is good.

cosmic moss
#

yeah I was wondering if I'll need to write my own binary parser or not

slender jay
#

I don't know if they can be reused

cosmic moss
#

haven't looked up. Still need to grok cpu parallelism

slender jay
#

But you can learn some ideas by reading a production-ready implementation.

#

Bonus; you're learning Go as well, because those packages are used to parse Go programs.

#

😄

cosmic moss
slender jay
#

Hmmm...
This doesn't look dissimilar from a chunky byte stream parser I encountered in Scala.

If I had to guess, a similar thing in Go is a map from chan byte to chan []byte? 🤔
I'm only skimming the Haskell though.

#

Maybe check out bufio.Scanner.

cosmic moss
#

ScanBytes is probably towards what I'm after. Maybe I'll just roll my own and only have the features I need :p

#

elf and pe are pretty awful to parse so it could be nice to get that done prior

cosmic moss
#

/solved

#

\solved