constant

package
v0.0.0-...-4cc5765 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Invalid = Kind(token.Invalid)

	// types for typed constants
	Bool       = Kind(token.Bool)
	Int        = Kind(token.Int)
	Int8       = Kind(token.Int8)
	Int16      = Kind(token.Int16)
	Int32      = Kind(token.Int32)
	Int64      = Kind(token.Int64)
	Uint       = Kind(token.Uint)
	Uint8      = Kind(token.Uint8)
	Uint16     = Kind(token.Uint16)
	Uint32     = Kind(token.Uint32)
	Uint64     = Kind(token.Uint64)
	Uintptr    = Kind(token.Uintptr)
	Float32    = Kind(token.Float32)
	Float64    = Kind(token.Float64)
	Complex64  = Kind(token.Complex64)
	Complex128 = Kind(token.Complex128)
	String     = Kind(token.String)

	// types for untyped constants
	UntypedBool    = Kind(token.UntypedBool)
	UntypedInt     = Kind(token.UntypedInt)
	UntypedRune    = Kind(token.UntypedRune)
	UntypedFloat   = Kind(token.UntypedFloat)
	UntypedComplex = Kind(token.UntypedComplex)
	UntypedString  = Kind(token.UntypedString)
	UntypedNil     = Kind(token.UntypedNil)

	// aliases
	Byte = Kind(token.Byte)
	Rune = Kind(token.Rune)
)

Variables

View Source
var (
	ErrInvalid = ErrorInvalid{}
)

Functions

This section is empty.

Types

type ErrorInvalid

type ErrorInvalid struct {
}

func (ErrorInvalid) Error

func (e ErrorInvalid) Error() string

type ErrorOverflow

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

func (ErrorOverflow) Error

func (e ErrorOverflow) Error() string

type ErrorType

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

func (ErrorType) Error

func (e ErrorType) Error() string

func (ErrorType) Type

func (e ErrorType) Type() *types.Complete

type Kind

type Kind = token.Kind

alias for token.Kind

type Value

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

equivalent to go/constant.Value the zero value i.e. Value{} has .Type() == nil, .Kind() = Invalid and .Err() = ErrInvalid

func BinaryOp

func BinaryOp(xv Value, op token.Token, yv Value) Value

replaces go/constant.BinaryOp() go/constant.Compare() and go/constant.Shift()

func Make

func Make(t *types.Complete, x interface{}) Value

*

  • Make creates a constant. Allowed x types depend on t.Kind():
  • bool if kind is Bool or UntypedBool
  • string if kind is String or UntypedString
  • int*, uint* or *math/big.Int if kind is Int*, Uint*, UntypedInt or UntypedRune
  • float32, float64, *big.Float or *big.Rat if kind is Float* or UntypedFloat
  • complex64 or complex128 if kind is Complex* or UntypedComplex
  • nil if kind is Invalid or UntypedNil *
  • return Value{Invalid, ErrorType} if conversion of val to kind is not allowed.
  • return Value{Invalid, ErrorOverflow} if conversion of val to kind is not exact.

func MakeFromLiteral

func MakeFromLiteral(lit string, tok token.Token) Value

func MakeImag

func MakeImag(v Value) Value

*

  • Create the complex constant i * c

func MakeKind

func MakeKind(kind Kind, x interface{}) Value

*

  • MakeKind creates a constant.
  • It is a shortcut for Make(types.BasicType(kind), x)

func MakeZero

func MakeZero(t *types.Complete) Value

func UnaryOp

func UnaryOp(op token.Token, v Value) Value

equivalent to go/constant.UnaryOp()

func (Value) Bool

func (v Value) Bool() bool

*

  • Bool returns the bool value of v.
  • v.Kind() must be Bool or Invalid

func (Value) Complex128

func (v Value) Complex128() (complex128, bool)

*

  • Complex128 returns the complex128 value of v and whether the result is exact;
  • v.Kind() must be Int*, Uint*, Float*, Complex*,
  • UntypedInt, UntypedRune, UntypedFloat, UntypedComplex or Invalid

func (Value) Complex64

func (v Value) Complex64() (complex64, bool)

*

  • Complex64 returns the complex64 value of v and whether the result is exact;
  • v.Kind() must be Int*, Uint*, Float*, Complex*,
  • UntypedInt, UntypedRune, UntypedFloat, UntypedComplex or Invalid

func (Value) DefaultType

func (v Value) DefaultType() *types.Complete

if v is typed, return Type() if v is untyped, return its default type

func (Value) Den

func (v Value) Den() Value

return denominator of Value. v.Kind() must be Int*, Uint*, Float*, UntypedInt, UntypedRune, UntypedFloat or Invalid

func (Value) Err

func (v Value) Err() error

*

  • Err returns the error of an Invalid constant.
  • returns nil if the constant is valid.

func (Value) Float32

func (v Value) Float32() (float32, bool)

*

  • Float32 returns the float32 value of v and whether the result is exact;
  • v.Kind() must be Int*, Uint*, Float*, UntypedInt, UntypedRune, UntypedFloat or Invalid

func (Value) Float64

func (v Value) Float64() (float64, bool)

*

  • Float64 returns the float64 value of v and whether the result is exact;
  • v.Kind() must be Int*, Uint*, Float*, UntypedInt, UntypedRune, UntypedFloat or Invalid

func (Value) Imag

func (v Value) Imag() Value

return imaginary part of Value v. v.Kind() must be Int*, Uint*, Float*, Complex*, UntypedInt, UntypedRune, UntypedFloat, UntypedComplex or Invalid

func (Value) Int64

func (v Value) Int64() (int64, bool)

*

  • Int64 returns the int64 value of v and whether the result is exact;
  • v.Kind() must be Int*, Uint*, UntypedInt, UntypedRune or Invalid

func (Value) IsValid

func (v Value) IsValid() bool

*

  • IsValid returns true if kind is != Invalid.

func (Value) Kind

func (v Value) Kind() Kind

func (Value) Num

func (v Value) Num() Value

return numerator of Value v. v.Kind() must be Int*, Uint*, Float*, UntypedInt, UntypedRune, UntypedFloat or Invalid

func (Value) Real

func (v Value) Real() Value

return real part of Value v. v.Kind() must be Int*, Uint*, Float*, Complex*, UntypedInt, UntypedRune, UntypedFloat, UntypedComplex or Invalid

func (Value) String

func (v Value) String() string

* return a short, user-readable string representation of v

func (Value) StringVal

func (v Value) StringVal() (string, bool)

*

  • StringVal returns the string value of v;
  • v.Kind() must be String, UntypedString or Invalid

func (Value) To

func (v Value) To(t *types.Complete) Value

*

  • try to convert Value v to to specified type.
  • return Value{Invalid, ErrorBadKind} if conversion is not allowed.
  • return Value{Invalid, ErrorOverflow} if conversion is not exact.
  • otherwise return converted Value

func (Value) ToKind

func (v Value) ToKind(kind Kind) Value

*

  • try to convert Value v to to specified kind.
  • return Value{Invalid, ErrorBadKind} if conversion is not allowed.
  • return Value{Invalid, ErrorOverflow} if conversion is not exact.
  • otherwise return converted Value

func (Value) Type

func (v Value) Type() *types.Complete

func (Value) Uint64

func (v Value) Uint64() (uint64, bool)

*

  • Uint64 returns the int64 value of v and whether the result is exact;
  • v.Kind() must be Int*, Uint*, UntypedInt, UntypedRune or Invalid

func (Value) Value

func (v Value) Value() interface{}

*

  • get the value of a constant. returned type is:
  • bool if Kind is Bool
  • string if Kind is String
  • int64 or *math/big.Int if Kind is Int*, Uint*, UntypedInt or UntypedRune
  • *big.Float or *big.Rat if Kind is Float* or UntypedFloat
  • nil if Kind is Invalid, Complex*, UntypedComplex or UntypedNil

Jump to

Keyboard shortcuts

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