Last active
May 24, 2021 09:41
-
-
Save genkuroki/e83b402686a20d3b33450b55f50b4743 to your computer and use it in GitHub Desktop.
How to construct a singleton with properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "https://discourse.julialang.org/t/implementing-singleton-design-pattern/61704" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module A\n\nstruct Singleton end\nconst singleton = Singleton()\n\nBase.getproperty(::Singleton, s::Symbol) = getproperty_singleton(Val(s))\ngetproperty_singleton(::Val{:nrows}) = 0\ngetproperty_singleton(::Val{:ncols}) = 0\ngetproperty_singleton(::Val{:rep}) = [0.0]\n\nBase.propertynames(::Singleton) = [:nrows, :ncols, :reo]\n\nfunction Base.show(io::IO, x::Singleton)\n print(io, \"Singleton(nrows = \", x.nrows, \", ncols = \", x.ncols, \", rep = \", x.rep, ')')\nend\n\nend\n\n@show A.singleton === A.Singleton() === A.Singleton()\n@show A.singleton\n@show A.singleton.nrows, A.singleton.ncols, A.singleton.rep;", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "A.singleton === A.Singleton() === A.Singleton() = true\nA.singleton = Singleton(nrows = 0, ncols = 0, rep = [0.0])\n(A.singleton.nrows, A.singleton.ncols, A.singleton.rep) = (0, 0, [0.0])\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module B\n\nusing GenerateProperties\n\nstruct Singleton end\nconst singleton = Singleton()\n\n@generate_properties Singleton begin\n @get nrows = 0\n @get ncols = 0\n @get rep = [0.0]\nend\n\nfunction Base.show(io::IO, x::Singleton)\n print(io, \"Singleton(nrows = \", x.nrows, \", ncols = \", x.ncols, \", rep = \", x.rep, ')')\nend\n\nend\n\n@show B.singleton === B.Singleton() === B.Singleton()\n@show B.singleton\n@show B.singleton.nrows, B.singleton.ncols, B.singleton.rep;", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "B.singleton === B.Singleton() === B.Singleton() = true\nB.singleton = Singleton(nrows = 0, ncols = 0, rep = [0.0])\n(B.singleton.nrows, B.singleton.ncols, B.singleton.rep) = (0, 0, [0.0])\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/e83b402686a20d3b33450b55f50b4743" | |
}, | |
"gist": { | |
"id": "e83b402686a20d3b33450b55f50b4743", | |
"data": { | |
"description": "How to construct a singleton with properties", | |
"public": true | |
} | |
}, | |
"kernelspec": { | |
"name": "julia-1.7-depwarn-o3", | |
"display_name": "Julia 1.7.0-DEV depwarn -O3", | |
"language": "julia" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.7.0" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment