top | item 39278476

In Go, I'm going to avoid using 'any' as an actual type

2 points| teichmann | 2 years ago |utcc.utoronto.ca

3 comments

order

behnamoh|2 years ago

Don't "go" that far!

Jokes aside tho, I agree—the "anything goes" (pun intended) approach in dynamic programming languages is so annoying. Using "any" in a typed language is like "go"ing back to dynamic style, which many already are trying to escape.

Enough puns for today!

mrkeen|2 years ago

Is there a difference in how or whether 'any' and 'interface{}' preserve static type information?

i.e., if I implement `identity(a) = a` in a generic way, can I still call identity(duck).quack()?

nh23423fefe|2 years ago

yes

  // You can edit this code!
  // Click here and start typing.
  package main

  import "fmt"

  func main() {
   duck := Duck{}

   identity(duck).quack()
  }

  func identity[T any](t T) T {
   return t
  }

  type Duck struct{}

  func (d Duck) quack() {
   fmt.Println("quack")
  }

  /*
  quack

  Program exited.
  */