Settings

Theme

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

utcc.utoronto.ca

2 points by teichmann 2 years ago · 3 comments

Reader

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.
      */

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection