Last active
May 17, 2021 16:49
-
-
Save genkuroki/8d67306f726bef5fbcf83e925b54f6d3 to your computer and use it in GitHub Desktop.
inner constructors
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": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module A\n\nBase.@kwdef struct Foo{T<:AbstractFloat}\n a::T = 1.0\n b::T = 2.0\nend\n\nend", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 1, | |
"data": { | |
"text/plain": "Main.A" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo()", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo(1.0, 2.0)", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo{Float64}(1.0, 2)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo(1.0, 2)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "LoadError", | |
"evalue": "MethodError: no method matching Main.A.Foo(::Float64, ::Int64)\n\u001b[0mClosest candidates are:\n\u001b[0m Main.A.Foo(::T, \u001b[91m::T\u001b[39m) where T<:AbstractFloat at In[1]:4", | |
"traceback": [ | |
"MethodError: no method matching Main.A.Foo(::Float64, ::Int64)\n\u001b[0mClosest candidates are:\n\u001b[0m Main.A.Foo(::T, \u001b[91m::T\u001b[39m) where T<:AbstractFloat at In[1]:4", | |
"", | |
"Stacktrace:", | |
" [1] top-level scope", | |
" @ In[5]:1", | |
" [2] eval", | |
" @ .\\boot.jl:369 [inlined]", | |
" [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", | |
" @ Base .\\loading.jl:1097" | |
] | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo(1f0, Int32(2))", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "LoadError", | |
"evalue": "MethodError: no method matching Main.A.Foo(::Float32, ::Int32)\n\u001b[0mClosest candidates are:\n\u001b[0m Main.A.Foo(::T, \u001b[91m::T\u001b[39m) where T<:AbstractFloat at In[1]:4", | |
"traceback": [ | |
"MethodError: no method matching Main.A.Foo(::Float32, ::Int32)\n\u001b[0mClosest candidates are:\n\u001b[0m Main.A.Foo(::T, \u001b[91m::T\u001b[39m) where T<:AbstractFloat at In[1]:4", | |
"", | |
"Stacktrace:", | |
" [1] top-level scope", | |
" @ In[6]:1", | |
" [2] eval", | |
" @ .\\boot.jl:369 [inlined]", | |
" [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)", | |
" @ Base .\\loading.jl:1097" | |
] | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module A\n\nBase.@kwdef struct Foo{T<:AbstractFloat}\n a::T = 1\n b::T = 2\n Foo{T}(A, B) where T<:AbstractFloat = new{T}(T.((A, B))...) \n function Foo(A, B)\n a, b = float.(promote(A, B))\n new{typeof(a)}(a, b)\n end\nend\n\nend", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "WARNING: replacing module A.\n", | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 7, | |
"data": { | |
"text/plain": "Main.A" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo()", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo(1.0, 2.0)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo{Float64}(1., 2)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo(1.0, 2)", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A.Foo(1f0, Int32(2))", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "Main.A.Foo{Float32}(1.0f0, 2.0f0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module A\n\nBase.@kwdef mutable struct Foo{T<:AbstractFloat}\n a::T = 1\n b::T = 2\n Foo{T}(A, B) where T<:AbstractFloat = new{T}(T.((A, B))...) \n function Foo(A, B)\n a, b = float.(promote(A, B))\n new{typeof(a)}(a, b)\n end\n Foo{T}(::UndefInitializer) where T<:AbstractFloat = new{T}()\nend\nFoo(::UndefInitializer) = Foo{Float64}(undef)\n\nend", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "WARNING: replacing module A.\n", | |
"name": "stderr" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 13, | |
"data": { | |
"text/plain": "Main.A" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "foo = A.Foo{Float64}(undef)", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 14, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(2.40312305e-315, 1.0e-323)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "foo.a, foo.b = 1, 2\nfoo", | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 15, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "bar = A.Foo(undef)", | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 16, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(2.607006273e-315, 1.075369206e-314)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "bar.a, bar.b = 1, 2\nbar", | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 17, | |
"data": { | |
"text/plain": "Main.A.Foo{Float64}(1.0, 2.0)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.7-depwarn-o3", | |
"display_name": "Julia 1.7.0-DEV depwarn -O3", | |
"language": "julia" | |
}, | |
"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 | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.7.0" | |
}, | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"gist": { | |
"id": "8d67306f726bef5fbcf83e925b54f6d3", | |
"data": { | |
"description": "inner constructors", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/8d67306f726bef5fbcf83e925b54f6d3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment