InterviewSolution
| 1. |
How Can I Modify The Declaration Of A Type In My Session? |
|
Answer» Perhaps you've defined a type and then realize you need to add a new field. If you try this at the REPL, you get the error: ERROR: invalid REDEFINITION of constant MyType Types in module Main cannot be redefined. While this can be INCONVENIENT when you are DEVELOPING new code, there's an excellent workaround. Modules can be replaced by redefining them, and so if you wrap all your new code inside a module you can redefine types and constants. You can't import the type names into Main and then expect to be able to redefine them there, but you can use the module name to resolve the scope. In other words, while developing you might use a workflow something like this: include("mynewcode.jl") # this defines a module MyModule OBJ1 = MyModule.ObjConstructor(a, b) obj2 = MyModule.somefunction(obj1) # Got an error. Change something in "mynewcode.jl" include("mynewcode.jl") # reload the module obj1 = MyModule.ObjConstructor(a, b) # old objects are no longer valid, must reconstruct obj2 = MyModule.somefunction(obj1) # this time it worked! obj3 = MyModule.someotherfunction(obj2, c) ... Perhaps you've defined a type and then realize you need to add a new field. If you try this at the REPL, you get the error: ERROR: invalid redefinition of constant MyType Types in module Main cannot be redefined. While this can be inconvenient when you are developing new code, there's an excellent workaround. Modules can be replaced by redefining them, and so if you wrap all your new code inside a module you can redefine types and constants. You can't import the type names into Main and then expect to be able to redefine them there, but you can use the module name to resolve the scope. In other words, while developing you might use a workflow something like this: include("mynewcode.jl") # this defines a module MyModule obj1 = MyModule.ObjConstructor(a, b) obj2 = MyModule.somefunction(obj1) # Got an error. Change something in "mynewcode.jl" include("mynewcode.jl") # reload the module obj1 = MyModule.ObjConstructor(a, b) # old objects are no longer valid, must reconstruct obj2 = MyModule.somefunction(obj1) # this time it worked! obj3 = MyModule.someotherfunction(obj2, c) ... |
|