#Random number generator for a generic type
70 messages · Page 1 of 1 (latest)
how does a random number work for a pointer to a string or struct or something like that
can't we declare a constraint for the types we already have in math/rand ?
you'd have to make one yourself, yeah
if not can i even make one ?
so i can i guess
isn't it just gonna be a type switch to the math/rand functions ?
if you wait long enough, they might make the functions in math/rand generic
hope they do i guess
then that's not really generic and not better than switching on the type of an interface
https://pkg.go.dev/crypto/[email protected]#Read Read sizeof bytes
Then unmarshal using https://pkg.go.dev/encoding/binary#Read
Or something along those lines
hmmmm 
Data must be a pointer to a fixed-size value or a slice of fixed-size values.
yeah slices won't work
thanks for the sweet examples
i didn't know about the rand.Reader nor binary.BigEndian
yeah i saw
both will give you random crap you have to sanitize (or not, if you wanna fuzz)
exactly
you most likely want to do something where you reflect on the fields, and then fill each field individually at runtime
bools can just be a single u8 you field%2==0 so they're true ~50% of the time
yeah something like that
but that could because a type switcher as well 
well, if you're planning to do that anyways, might as well using something like that https://github.com/go-faker/faker
Go (Golang) Fake Data Generator for Struct, previously https://github.com/bxcodec/faker - GitHub - go-faker/faker: Go (Golang) Fake Data Generator for Struct, previously https://github.com/bxcodec/...
i'll try to generelize most of the stuff
what's your end goal, really?
idk i just had this idea about random number which would have a generic input
i am not really trying to make something which i need now
first i was just thinking of numbers only
so that i could have some tighter control maybe
which begs another question is there like a way to compare types b/n constraints
like if i use
func ff(inp any / comparable)
and have a constraint named Number with all the types of ints and floats
can i take inp and check if it can be assigned to a Number or not ?
cause then i could like have this whole thing which adds random crap to everything and branch out the nunmbers to a different function
@next elbow
type Integer interface {
Signed | Unsigned
}
Integer is a constraint that permits any integer type. If future releases of Go add new predeclared integer types, this constraint will be modified to include them.
wow we can have Integer Type
its not a type it's a constraint
ohh yeah
so can we ?
like check if some type of variable falls under Integer constraint ?
i mean
ohh
thanks
but like if i sent string to a function with any or comaparable constraint can i then check if it will fall under other constraints or not ? like we can type check other vars
I dont think it's possible
Essentially, type constraints only exist at comptime
They hold concrete values
You can't do runtime logic on comptime values
hmmm
thanks for the explanation
the PrintInt function there is monomorphized to fit any kind of int you give it, v is not boxed
(because the constraints are small values, in practice monomorphization isn't guaranteed)
hmmmmmm
This is the full doc on how it works
but essentially, the function is compiled once for each different type you pass in (unless that type is fat)
You can't decide which implementation of the function you want at runtime
hmmm got it
although i don't have much clue what monomorphization means
seems a mouthful term
In programming languages, monomorphization is a compile-time process where polymorphic functions are replaced by many monomorphic functions for each unique instantiation. This transformation is desirable, since then the output intermediate representation (IR) will have concrete types and can be optimized better. Furthermore, most IRs are designe...
It's quite simple in theory
give a function an int, the compiler generates the implementation for an int
give it a string, it generates impl for string
hmmm
as you said its the compile time thing actually lets the generic types have the concrete type in each run of the function
in case it wasn't clear, yes you cannot do type assertions on parametric types but you can do runtime checks on them, with caveats go func PrintInt[T ~int](v T) { switch any(v).(type) { case int: log.Println("int") default: log.Println("not int") } }
would print "not int" for a type myint int
func PrintInt[T ~int](v T) {
if reflect.TypeOf(v).Kind() == reflect.Int {
log.Println("int")
} else {
log.Println("not int")
}
}```
would print "int" for both
wow thanks for the consideration
i see so type switching can check the some kind of overlay types
in other hand the reflect can check the underlying type even if the are type aliased (or whatever they are called )
what's ~int tho ?
I forget the "proper" name but it means "any type whose underlying type is int"
ohh cool
there is an open proposal to allow type assertions on parametric types but for now this is the best we can do (box in an any and assert on that / use reflect)