How to make fields in type object undefined


#1

Hi folks,
in many of my julia codes I define my own type variables, in such a way that nothing is by default initialized, expecting the user will initialize the fields he needs. For instance

type fooo
    x :: Int
    y :: Float64
    z :: Int
    t :: Float64
    fooo() = new()
end;

and then

kkk = fooo()
kkk.x = 10
kkk

gives as result

fooo(10,6.90533903150177e-310,139765626155696,6.90534204103743e-310)

while, obviously

isdefined(kkk,:x)

yields

true

but

isdefined(kkk,:y)

also yields

true

because some values have been set to kkk.y. My question is: is there a way to get to know that kkk.x has been given a value while kkk.y contains garbage and has not been given any value by the user?

Best regards and thanks,

Ferran.


#2

Nope. Consider changing your type fields to Nullables and then use isnull.


#3

You can use a “sentinel value”, i.e. a special value that is not valid for your use case. For example, that could be -1 for integer fields if they must be non-negative, or NaN for Floats.


#4

Not for primitive types. Think about it: since every bit pattern is a meaningful value, it is impossible to know how it got there.

As @dpsanders said, sentinel values are in general a possible solution, but if you tell us more about your problem, you might get a more specific suggestion.