base

package
v0.31.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package base describes the Base struct, which provides virtual functions and inheritance similar to that of C++ and Java.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Embedded added in v0.29.0

func Embedded[T BaseI](o BaseI) T

Embedded returns the structure of type T that is embedded in o.

T should be a pointer type to the embedded structure and be public. The returned structure will bypass virtual functions that are defined in o and call functions directly on the embedded structure. It will also have access to all the private members of o.

Will panic if T is not found in o.

Another way to do this, and perhaps better in certain circumstances, is to create a private method on both the object and interface to the object that simply returns the object.

For example. using the Bird-Duck example:

	type BirdI interface {
	  BaseI
	  Call() string
	  self() *Bird
	}

 func (a* Bird) self() *Bird {
	  return a
	}

This can be very helpful in special situations, like if you want access to private members from recursive structures.

For example:

  type Bird struct {
    Base
    chicks []BirdI
    wasFed bool
	 }

You can do this:

   func (a *Bird) FeedChicks () string {
	    for chick := range a.self().chicks {
	      chick.self().wasFed = true
     }
	  }

And the FeedChicks() function will work on any type of Bird class, without having to expose the internals of Birds, or create more private methods on the Bird interface just to get access to the internals of Bird.

Types

type Base

type Base struct {
	// contains filtered or unexported fields
}

func (*Base) Init

func (b *Base) Init(self any)

Init captures the object being passed to it so that virtual functions can be called on it.

func (*Base) Self

func (b *Base) Self() any

Self returns the self as an interface object.

func (*Base) String added in v0.29.0

func (b *Base) String() string

String implements the Stringer interface.

This default functionality outputs the object's type. String is overridable.

func (*Base) TypeOf added in v0.29.0

func (b *Base) TypeOf() reflect.Type

TypeOf returns the reflection Type of self.

type BaseI

type BaseI interface {
	// TypeOf returns the reflection type of the object. This allows superclasses to determine
	// the type of the subclassing object. The type will not be a pointer to the type, but the type itself.
	TypeOf() reflect.Type
	// String returns a string representation of the object.
	String() string
}

BaseI is an interface representing all objects that embed the Base struct.

It adds the ability for all such objects to get the object's type and a string representation of the object, which by default is the object's type. To be sure to get the object's type even if String() is overridden, use TypeOf().String().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL