Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dpsanders/1ef8f329d6d56c1c9a86 to your computer and use it in GitHub Desktop.
Save dpsanders/1ef8f329d6d56c1c9a86 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"move! (generic function with 2 methods)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"abstract Person\n",
"\n",
"type Adult <: Person\n",
" age::Int\n",
" height::Float64\n",
" position::Int\n",
"end\n",
"\n",
"type Child <: Person\n",
" age::Int\n",
" height::Float64\n",
" position::Int\n",
" at_school::Bool\n",
"end\n",
"\n",
"type Bad <: Person\n",
" age::Int\n",
"end\n",
"\n",
"function move!(p::Person)\n",
" p.position += 1\n",
"end\n",
"\n",
"function move!(c::Child)\n",
" c.position += 1\n",
" c.at_school = !c.at_school\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Adult(3,4.0,5)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = Adult(3, 4, 5)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"move!(a)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Child(3,4.0,5,true)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = Child(3, 4, 5, true)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"move!(c)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Bad(3)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = Bad(3)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "LoadError",
"evalue": "type Bad has no field position\nwhile loading In[11], in expression starting on line 1",
"output_type": "error",
"traceback": [
"type Bad has no field position\nwhile loading In[11], in expression starting on line 1",
"",
" in move! at In[8]:21"
]
}
],
"source": [
"move!(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.3.11-pre",
"language": "julia",
"name": "julia-0.3"
},
"language_info": {
"name": "julia",
"version": "0.3.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment