Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you create a singleton in Julia?

Tags:

julia

I know that Julia does not have OOP but that multiple dispatch enables similar ideas. Given how seemingly contentious the use of singletons are in Python, I am wondering if there is a similar idea Julia (i.e. a struct that can only be instantiated once).

I am wondering if there's a way to have the constructor keep track of the number of times an object was instantiated with a global var or something like that? Or it's altogether not be possible?

like image 729
logankilpatrick Avatar asked Nov 16 '25 16:11

logankilpatrick


1 Answers

The main way people make singletons in Julia is to define an empty struct (which means that it has 0 size), and define methods that return information for it.

struct Singleton
end
data(::Singleton) = "whatever you want it to do"

etc.

like image 126
Oscar Smith Avatar answered Nov 18 '25 09:11

Oscar Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!