[julia-users] Overloading +=
Is it possible that + can be overloaded, but += cannot (but is derived from +)? My context is this: I want to define some type, say `mytype` and allow the operation A = mytype() A[i] += B where i is some index, B is some value. But I do *not* want to allow something like A = A + B At the moment, I just overload setindex!, but then I need to use A[i] = B which reads all wrong. Is there a way to do what I want? Thank you, Christoph
[julia-users] Overloading `eval`
Do I understand correctly that `eval` cannot be overloaded? I get the following message (0.3.9): Warning: import of Base.eval into Lattices conflicts with an existing identifier; ignored. Thanks, Christoph
[julia-users] Overloading convert
Here is a simplified version of a DBI type scheme, almost entirely using convert function overloads. I temporarily have eval_foreign instead of eval, because I cannot figure out how to overload Base.eval successfully. cd("/home/haldane/Julia_package") using DataFrames using SQLite using Lazy import Base.convert type DataBaseTable conn::SQLiteDB name::String end type CsvFile file::String end function convert(::Type{DataBaseTable}, conn::SQLiteDB, name::String, df::DataFrame) create(conn, name, df, @>> df names map(string) ) DataBaseTable(conn, name) end function convert(::Type{SQLiteDB}, string::String) SQLiteDB(string) end function eval_foreign(db::SQLiteDB, code::String) query(db, code) end function convert(::Type{Dict}, conn::SQLiteDB) names = @as _ begin "select name from sqlite_master where type = 'table'" eval_foreign(conn, _) _.values[1] end [symbol(name) => DataBaseTable(conn, name) for name in names] end function convert(::Type{DataFrame}, dbt::DataBaseTable) answer = eval_foreign(dbt.conn, "select * from $(dbt.name)" ) df = DataFrame(answer.values) @>> answer.colnames map(symbol) names!(df) end function convert(::Type{CsvFile}, name::String, df::DataFrame) writetable(name, df) CsvFile(name) end function convert(::Type{DataFrame}, file::String) readtable(file) end function convert(::Type{DataFrame}, cf = CsvFile) readtable(cf.file) end conn = @>> "my_database.db" convert(SQLiteDB) A = DataFrame(a = 1, b = 2) B = DataFrame(a = 1, b = 2) @>> A convert(DataBaseTable, conn, "A") @>> B convert(DataBaseTable, conn, "B") tables = @>> conn convert(Dict) A = @>> begin tables[:A] convert(DataFrame) convert(CsvFile, "A.csv") convert(DataFrame) end
Re: [julia-users] Overloading +=
> Is it possible that + can be overloaded, but += cannot (but is derived from > +)? Yes, that is right. += is just sugar. There has been talk to make += stand on its own. > My context is this: > > I want to define some type, say `mytype` and allow the operation > A = mytype() > A[i] += B > where i is some index, B is some value. > > But I do *not* want to allow something like > A = A + B > > At the moment, I just overload setindex!, but then I need to use A[i] = B > which reads all wrong. I don't quite understand. A[i] += B gets translated to A[i] = A[i] + B So you need to define both getindex and setindex! for A, and + needs to be defined for the elements of A & B. Then it should work.
Re: [julia-users] Overloading +=
Thank you. I think making += standalone would be good. I assume this would also make it faster? Re your suggestion: yes, I do understand that I can do that (in fact I did do it that way), but this makes it possible to evaluate A[i] and work with it, which I want to expressly forbid in my code. This is not "mission-critical", just academic really, I was just curious whether it could be done. Thanks for your help. Christoph
Re: [julia-users] Overloading +=
See the discussions at: https://github.com/JuliaLang/julia/issues/249 https://github.com/JuliaLang/julia/issues/3217 https://github.com/JuliaLang/julia/issues/7052
[julia-users] overloading assignment operator
Hi, is it possible to overload the assignment operator in Julia like in C++. I cannot really find information whether this is possible or not and if not why not and if there is an alternative. Thanks, Thorsten
[julia-users] overloading assignment operator
The answer is basically no. Assignment to an identifier always creates a new binding for that identifier, possibly after converting the right hand side to the type of the variable in question. For an object x, x[inds...] = a can be overloaded (this is the setindex! function). At some point it might become possible to overload x.y = a As for alternatives: What is your use case?
Re: [julia-users] Overloading `eval`
You can define a function called "eval" in a baremodule, but you will then need to explicitly import the other things you might need from Base ... what is the goal? On Jun 18, 2015 8:31 AM, "Christoph Ortner" wrote: > Do I understand correctly that `eval` cannot be overloaded? > > I get the following message (0.3.9): > > Warning: import of Base.eval into Lattices conflicts with an existing > identifier; ignored. > > Thanks, > Christoph > >
Re: [julia-users] Overloading `eval`
On Thu, Jun 18, 2015 at 9:21 AM, Isaiah Norton wrote: > You can define a function called "eval" in a baremodule, but you will then > need to explicitly import the other things you might need from Base ... what > is the goal? > > On Jun 18, 2015 8:31 AM, "Christoph Ortner" > wrote: >> >> Do I understand correctly that `eval` cannot be overloaded? >> >> I get the following message (0.3.9): >> >> Warning: import of Base.eval into Lattices conflicts with an existing >> identifier; ignored. The eval function is defined in the module itself[1] and Core.eval is not a generic function. http://julia.readthedocs.org/en/release-0.3/manual/modules/?highlight=baremodule#default-top-level-definitions-and-bare-modules >> >> Thanks, >> Christoph >> >
Re: [julia-users] Overloading `eval`
I didn't fully understand this: are you saying that, unless I use `baremodule`, I cannot overload `eval`? my usage is simply that I have type A, say, that defines a function and I want to call eval(A, arg) to evaluate. I am perfectly happy to just call `feval` instead of `eval`, but I was curious. Many thanks, Christoph
Re: [julia-users] Overloading `eval`
Yes, I don't think there is a way to replace a module's eval function. At least, I can't think of any way to accomplish this besides creating a baremodule. On Thu, Jun 18, 2015 at 11:35 AM, Christoph Ortner < christophortn...@gmail.com> wrote: > I didn't fully understand this: are you saying that, unless I use > `baremodule`, I cannot overload `eval`? > > my usage is simply that I have type A, say, that defines a function and I > want to call eval(A, arg) to evaluate. I am perfectly happy to just call > `feval` instead of `eval`, but I was curious. > > Many thanks, > Christoph >
Re: [julia-users] Overloading `eval`
I skipped a step: as Yichao said, the built-in eval is not a generic function so it can't be extended. my usage is simply that I have type A, say, that defines a function and I > want to call eval(A, arg) to evaluate. I am perfectly happy to just call > `feval` instead of `eval`, but I was curious. It's still a little unclear what the goal is, and AFAIK there is no (built-in) `feval` in Julia. You might want constructors? http://julia.readthedocs.org/en/release-0.3/manual/constructors/ Also note that in 0.4-dev (github master) you will be able to make A directly callable, see: https://github.com/JuliaLang/julia/issues/2403 (and the implementation in #8712) On Thu, Jun 18, 2015 at 11:35 AM, Christoph Ortner < christophortn...@gmail.com> wrote: > I didn't fully understand this: are you saying that, unless I use > `baremodule`, I cannot overload `eval`? > > my usage is simply that I have type A, say, that defines a function and I > want to call eval(A, arg) to evaluate. I am perfectly happy to just call > `feval` instead of `eval`, but I was curious. > > Many thanks, > Christoph >
Re: [julia-users] Overloading `eval`
ok - thank you for clarifying! Christoph
Re: [julia-users] Overloading `eval`
Maybe you can name the function you need "evaluate", for example. David. El jueves, 18 de junio de 2015, 20:44:20 (UTC+2), Christoph Ortner escribió: > > ok - thank you for clarifying! > Christoph > >
[julia-users] overloading an abstract interface
I have a pattern that has come up multiple times where in a module I might have some function `foo` which will call some other function `bar` on a given type. The exact function `bar` isn't defined yet because it is defined by the user-type. The problem I run into is that if `bar` is defined outside of the module, `foo` won't see it. Here is a simple example: module testmod export foo, Baz abstract Baz bar(x::Baz) = nothing function foo(x::Baz) return bar(x) end end with the user-defined code: using testmod type ConcreteBaz <: Baz value::Int64 end function bar(baz::ConcreteBaz) return baz.value end baz = ConcreteBaz(42) println("result = $(foo(baz))") The output is `result = nothing`, and thus the wrong function is called. If I move all of the code to the same file it works. My question is: is this expected behavior? If so, what's the proper way of creating an interface for an abstract type which can later be overridden in a user file?
[julia-users] overloading an abstract interface
Yes, this is expected behavior. You will have to define testmodule.baz() to not create a new function, but extend the existing function with a new method.
[julia-users] Overloading "." by specializing getfield() / setfield!()?
I'm wondering if implementing getfield() / setfield!() for a composite type could be a good design for getters and setters. Say I have a type type Thing a :: Int b :: int end where I'm requiring the `a` field to be even. It'd be great to be able to type thing = Thing() thing.a = 3 and receive an error. In 0.3.10 and 0.4, I can't implement setfield!(): ERROR: cannot define function setfield!; it already has a value. Is there a way to do this?
[julia-users] Overloading Base functions or creating new ones?
Simple example. Let's say I have the type type Mesh elements::Vector{Element} node::Vector{Node} end and I want to add functions to add elements or nodes. I could either do this by overloading push! like this: push!(mesh::Mesh, elem::Element) = push!(mesh.elements, elem) or add a new method addelement(mesh::Mesh, elem::Element) = push!(mesh.elements, elem) I know code where operators are heavily overloaded can be difficult to read however, this case seems quite simple. Anyone have any advice for which way to prefer? What is more Julian? Best regards, Kristoffer Carlsson
[julia-users] Overloading a function with type not compiled yet
Hi, I have a module with a function that I'd like to be overloaded, one version should take as an argument a type that I declare somewhere else and later (i.e. this module is required before the argument type) and another version should be an Array of such a type. That's the idea. Simple example: I have three files. In my_module.jl: function f(s) println(s.x) end function f(sl::Array{Any}) for s in sl f(s) end end In my_type.jl: type state x y z end In my_main.jl: include("my_module.jl") include("my_type.jl") s1 = state(1.0, 5.0, 7.0) s2 = state(2.0, 6.0, 8.0) sl = [s1, s2] println("f(s1)") f(s1) println("f(s2)") f(s2) println("f(sl)") f(sl) If I run: julia my_main.jl I get this error: "ERROR: type Array has no field x" because it uses the first version of the function, nothing prevents it indeed. I would like to find a way to make it understand it has to choose the second version of the function. How could I do that? Thanks a lot,
Re: [julia-users] Overloading a function with type not compiled yet
On Thu, Sep 17, 2015 at 8:21 AM, wrote: > Hi, > > I have a module with a function that I'd like to be overloaded, one version > should take as an argument a type that I declare somewhere else and later > (i.e. this module is required before the argument type) and another version > should be an Array of such a type. That's the idea. > > Simple example: I have three files. > > In my_module.jl: > > function f(s) > println(s.x) > end > > function f(sl::Array{Any}) > for s in sl > f(s) > end > end function f{T}(sl::Array{T}) . end > > In my_type.jl: > > type state > x > y > z > end > > In my_main.jl: > > include("my_module.jl") > include("my_type.jl") > > s1 = state(1.0, 5.0, 7.0) > s2 = state(2.0, 6.0, 8.0) > sl = [s1, s2] > > println("f(s1)") > f(s1) > println("f(s2)") > f(s2) > println("f(sl)") > f(sl) > > If I run: julia my_main.jl > I get this error: "ERROR: type Array has no field x" because it uses the > first version of the function, nothing prevents it indeed. > > I would like to find a way to make it understand it has to choose the second > version of the function. How could I do that? > > Thanks a lot,
Re: [julia-users] Overloading a function with type not compiled yet
> > function f{T}(sl::Array{T}) > . > end > Worked like a charm, thank you. I don't really get why though, what mechanism prevents Julia to use the first version now? Does it look at all the available functions called f and see if one is better suited, is likely to produce an error of some sort, something else?
Re: [julia-users] Overloading a function with type not compiled yet
On Thursday, September 17, 2015 at 9:45:11 AM UTC-4, ami...@gmail.com wrote: > > I don't really get why though, what mechanism prevents Julia to use the > first version now? > Does it look at all the available functions called f and see if one is > better suited, is likely to produce an error of some sort, something else? > This is called parametric invariance. See here for more details: http://docs.julialang.org/en/release-0.4/manual/types/#parametric-composite-types A type theorist will be able to tell you lots more, but on a practical level, it allows `Array{Int}` to be stored *differently* than an `Array{Any}`. The latter is an array of pointers to other memory regions, whereas the former is just a contiguous chunk of raw `Int`s. This is crucially important for fast numeric arrays and interop with BLAS and other C libraries.
Re: [julia-users] Overloading a function with type not compiled yet
On Thu, Sep 17, 2015 at 9:45 AM, wrote: >> function f{T}(sl::Array{T}) >> . >> end > > > Worked like a charm, thank you. > I don't really get why though, what mechanism prevents Julia to use the > first version now? The reason is what Tom said. In julia ParameterizedType{A} is not a subtype of ParametrizedType{B} even if A <: B (i.e. A is a subtype of B) (with Tuple as the only exception now) See doc [1] (in particular the bold note following "the last point is very important"). In your example, you specify f(sl::Array{Any}) to be applicable to sl of type `Array{Any}`. However, the argument you supply is of type `Array{state}` which is not a subtype of `Array{Any}` so the method is not applicable. On the other hand, with `f{T}(sl::Array{T})`, the method matches when `T == state`. [1] http://julia.readthedocs.org/en/latest/manual/types/#parametric-composite-types > Does it look at all the available functions called f and see if one is > better suited, is likely to produce an error of some sort, something else?