-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redesign attribute.go #42
Comments
Abuse of Global VariablesThe following global variables are not needed, as they are simply another form of |
Types and Constants Should've HiddenNo need to define type |
To support the optional default value package main
import (
"log"
"reflect"
)
// Dictionary contains the description of all attributes
type Set struct {
d map[string]*description
recent string // Recently added name.
}
type description struct {
typ reflect.Type
val interface{}
doc string
chkr func(i interface{}) error
}
func NewSet() *Set {
return &Set{
d: make(map[string]*description),
recent: "",
}
}
// Int declares an attribute of int-typed in Dictionary d.
func (s *Set) Int(name string, doc string) *Set {
s.d[name] = &description{
typ: reflect.Type(int),
doc: doc,
}
s.recent = name
return s
}
func (s *Set) Default(v interface{}) *Set {
if t := reflect.TypeOf(v); t != s.d[s.recent].typ {
log.Fatalf("The type of %s is declared %v, but the default value %v has a different type %v", s.recent, s.d[s.recent].typ, v, t)
}
return s
}
func main() {
NewSet().Int("num_class", "Number of classes").Default(5) // Runs well.
NewSet().Int("num_class", "Number of classes").Default(1.5) // log.Fatal
} |
How about use optional package like https://pkg.go.dev/github.com/markphelps/optional?tab=doc ? The optional package is implemented like: // Int is an optional int.
type Int struct {
value *int
}
// NewInt creates an optional.Int from a int.
func NewInt(v int) Int {
return Int{&v}
}
func main() {
b1 := Int() // nil int
b2 := NewInt(0) // non nil int with value 0
} The attribute code may like: func (d Dictionary) Int(name string, defaultValue optional.Int,
doc string, checker func(int) error) {
...
}
var dict = Dictionary{}.
Int("num_class", optional.Int{}, "doc1", checker1). // default value is nil
Int("attr_with_default_value", optional.NewInt(0), "doc2", checker2) // default value is 0 |
I am afraid that I would prefer the
|
@wangkuiyi If we use #42 (comment), how about just write the
instead of 2(or 3) methods:
In this way, we can:
|
@sneaxiy I am afraid that we cannot. If we do so, users need to write var a = 1.0f
attribute.NewDictionary().Int("name", &a, ...) Go syntax doesn't allow him/her to write |
@wangkuiyi Go can convert primitive types like
|
Fantastic! I agree with your proposal. Thank you for letting us know this property of Go! |
@wangkuiyi The attribute package has been refined by the following PR:
Maybe we can close this issue now. |
Declare Attributes
Currently, users write the following code to declare attributes:
This forces us to expose types like
attribute.Description
which should be internal. Also, this exposes constants likeattribute.Int
, which should be hidden as well.Following the design of https://golang.org/pkg/flag/, the declaration should be
We must be very careful about the abuse of
interface {}
. In the current API, users can passing value as aninterface {}
type, which might not match the specified type.Also, users can pass in any
reflect.Type
as the type, not necessarily chosen from the pre-defined list.Changing to
Int("train.num_pos", 0, ...)
makes the compiler checks the type and value are matched.The text was updated successfully, but these errors were encountered: