abstract Animal
type Dog <: Animal
weight:: Real
end
type Retriever <: Dog
name::String
end
function A (p1:: Real, p2:: Int64) ...
function A (p1:: Int64, p2:: Real) ...
A(1,1)
abstract MyType
function foo(x::MyType)
x.mySuperField + x.mySuperDuperField
end
abstract Particle
function printPosition(p :: Particle)
println(p.pos)
end
In the github discussion the consensus seems to be that interfaces are necessary and fields on abstract types are problematic. I'm afraid I don't see the problem of abstract types with fields, since basically you already got this feature. What's the difference between demanding a field on an abstract type by using it implicitly in a function and adding it directly to the type ?
Adding the ability to specify fields for abstract types is what #4935 is about, but the whole issue kind of stalled out when we realized the feature isn't really buying you very much and may conflict with other, more useful features we want to add. My personal realization of this was here. Also note that specifying fields for abstract types is not the same as allowing subtyping of concrete types – the crucial distinction being that you still can't instantiate an abstract type.
abstract Vehicle
function getInfo ( v :: Vehivle)
println(v.name)
end
type Car <: Vehicle
weight :: Real
speed :: Real
prize :: Real
name :: String
end
type Bike <: Vehicle
weight :: Real
speed :: Real
prize :: Real
name :: String
end
type Boat <: Vehicle
weight :: Real
speed :: Real
prize :: Real
name :: String
end
type Retriever
weight :: Real
name :: String
end
type Tom <: Retriever
end
function feed ( r :: Retriever)
r.weight += 1
end
function sit ( t :: Tom )
println ( "good dog")
end
> Just as it makes no sense to subtype electrons, it makes no sense toReally?
> subtype Int (unless you want to get into slicing up the domain of
> Int, but that's a different matter entirely).
Natural numbers are specified by the Peano axioms. I'm not
> If a value is an Int, that already completely determines how it is
> represented and how it behaves.
aware of the fact that from that follows any conclusion about the
representation of natural numbers. It does also not follow what
operations are allowed or provided by natural numbers defined by the
peano axiom. If one wants to have +, define it, etc. If you want to
represent natural numbers via
0, S0, SS0, SSS0, ...
or
0, 1, 10, 11, ...
or
0, {0}, {0,{0}}, {0,{0,{0}}}, ...
is just a choice one has.
> Integer, on the other hand, is an an abstraction like "fermion" andAh, OK. You basically have the same opinion and Int is a specific Julia
> it makes sense to talk about specific kinds of Integer – precisely
> because saying that something is an Integer does not completely
> specify its representation and behavior.
type? No, you have Int8 up to Int128 as far as I can see. In other
words, Julia has nothing to represent natural numbers. Maybe I'm wrong,
but let's for the moment think that Int is the type that can hold any
integer.
Wouldn't it make sense to subclass it and specify all even integers?
That's just as well a ring and every operation would be the same, except
perhaps that even?(x) always returns true and that coercion of an
integer into an even integer might fail.
How would I program the type of all prime numbers in Julia? As I
understand, I cannot subtype Int for that. Are you saying that I can at
most get a function prime? that tests whether an integer is prime or not?
Indeed, Gtk.jl is one of the cutting edge projects in this regards and it reveals one thing that has not been mentioned in this thread and what is much more needed than subtyping concrete types: multiple inheritance.
In Java/C# it is possible to inherit from multiple interfaces and this is currently not possible in Julia. But the need for it might be very specific to Gtk.jl where the underlying Gtk C code supports multiple inheritance of interfaces. I know that Jamson is working on a workaround but still this might be an area where Julia is currently most limiting.
In the github discussion the consensus seems to be that interfaces are necessary and fields on abstract types are problematic. I'm afraid I don't see the problem of abstract types with fields, since basically you already got this feature. What's the difference between demanding a field on an abstract type by using it implicitly in a function and adding it directly to the type ?
> There are languages that encourage you to do this kind of thing with the> type system, but Julia is not one of them.OK.
But some people even want to compute with species, for example, multiply
them.
http://www.risc.jku.at/people/hemmecke/AldorCombinat/combinatsu24.html#x38-560008.11
Simpler things would be to form products of ideals or factor rings.
Hmm, sounds like Juilia is nothing for me.
# Mydict definition:
type Mydict{k, v} d::Dict{k, v} Mydict() = new(Dict())end
Mydict() = Mydict{Any, Any}()
function setindex!{k, v}(md::Mydict{k, v}, key, val) print("my dict is special!") md.d[key] = valend
convert{k, v}(::Type{Dict{k, v}}, md::Mydict{k, v}) = md.d
# Usage:
md = Mydict()
md[1]=2 # <--- prints 'mydict is special'
delete!(md, 1) # <-- currently doesn't work. It needs automatic conversion
Yes, that's what I mean, too.
And normal interfaces in Java or C# would behave differently. They can't inherit from each other. And they only document methods, not field
The only real world things I can think of that are non-singleton concrete types are species of atomic particles. There are a vast number of electrons in the universe and they are indistinguishable from each other: describing something as "an electron" is, to the best of scientific knowledge, a complete description of it. Classes of particles like "fermion" and "boson" are abstract types, of which there are many specific kinds. If I ask what type of particle something is and the response is "fermion," then I might follow up with "but what kind of fermion?", and so on, until I get to a specific kind of fermion, such as "electron". When I know something is an electron, there are no more questions to ask. "Fermion" is an incomplete specification of a particle so it makes sense to talk about subtypes of fermions, whereas "electron" is a complete specification of a particle and it doesn't make sense to talk about subtypes of electrons.