#Random number generator for a generic type

70 messages · Page 1 of 1 (latest)

barren talon
#

idk if ppl have started using generic types or not but just a had thought
isn't there any random number generator for a generic type ?
like you pass a pointer to a variable and it will assign a random number in that variable of correct type

if not can i even make one ?

lapis mango
#

how does a random number work for a pointer to a string or struct or something like that

barren talon
#

can't we declare a constraint for the types we already have in math/rand ?

lapis mango
#

you'd have to make one yourself, yeah

barren talon
#

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 ?

lapis mango
#

if you wait long enough, they might make the functions in math/rand generic

barren talon
#

hope they do i guess

lapis mango
barren talon
#

yeah

#

that's why the question can i even make a function which would be generic

next elbow
barren talon
#

hmmmm thinkingo

barren talon
#

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

next elbow
#

well, byte order doesn't matter here

#

it's all random

barren talon
#

yeah i saw

next elbow
#

both will give you random crap you have to sanitize (or not, if you wanna fuzz)

barren talon
#

yeah i will try to sanitize it

#

also the bool would be true for like 99%

next elbow
#

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

barren talon
#

yeah something like that
but that could because a type switcher as well gopherfacepalm

next elbow
barren talon
#

i'll try to generelize most of the stuff

next elbow
#

what's your end goal, really?

barren talon
#

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

drifting spadeBOT
#
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.

barren talon
#

wow we can have Integer Type

next elbow
#

its not a type it's a constraint

barren talon
#

ohh yeah

#

so can we ?
like check if some type of variable falls under Integer constraint ?

next elbow
#

the compiler does it

#

It's a comptime thing

barren talon
#

i mean

barren talon
#

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

next elbow
#

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

barren talon
#

hmmm
thanks for the explanation

next elbow
#

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)

barren talon
#

hmmmmmm

next elbow
#
#

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

barren talon
#

hmmm got it

#

although i don't have much clue what monomorphization means
seems a mouthful term

next elbow
#

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

barren talon
#

hmmm
as you said its the compile time thing actually lets the generic types have the concrete type in each run of the function

mental jungle
#

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
barren talon
mental jungle
#

I forget the "proper" name but it means "any type whose underlying type is int"

barren talon
#

ohh cool

mental jungle
#

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)