Re: How to create a subtype of a non-abstract supertype (aka how to create a subclass)

773 views
Skip to first unread message
Message has been deleted

Avik Sengupta

unread,
Mar 22, 2013, 1:51:01 PM3/22/13
to julia...@googlegroups.com
As you've already seen, you cannot subtype a non-abstract (aka composite) type. 

One way to achieve what you want is as follows

type SubType
  x::SuperType
  y::Float64
end

SubType(x, y) = SubType(SuperType(x), y)  #the constructor

The Julia union of types is very different from the C Union. In Julia, Union is really the OR operator for types. So if type A is defined as Union of type X and type Y, all that means is that an instance of A will be either an instance of X OR an instance of Y. 

Hope this helps. 

Regards
-
Avik

On Friday, 22 March 2013 16:39:44 UTC, Theodore Papamarkou wrote:
Let's say that I have a type such as

type SuperType
  x::Float64
end

and let's say that I want to create a subtype of the above non-abstract base SuperType, such as

type SubType <: SuperType
  y::Float64
end

I know that the operator <: is erroneously used above, I only used it in order to explain what I want to achieve. I want SybType to have two data members, its own y and the (inherited) data member x of SuperType. The problem is that <: is used only with abstract types and for boolean operations as far as my undrestanding goes.

The only alternative I can think of is to use the Union type; is this a possibility? If so, I would be grateful if anyone could give me an example.

Also, how initialization of x can occur from the constructor of SybType in the above example? Perhaps I am thinking in a C++ way, and I should start thinking more in Julia syntax directly.

Thanks a lot for the help!

Theodore Papamarkou

unread,
Mar 22, 2013, 2:56:25 PM3/22/13
to julia...@googlegroups.com
Thank you Avik!

Yes, I was aware of this solution that you suggested, which is not exactly in the C++ spirit, but it is conceptually equivalent (and does the job for me).

What I hadn't understood well was the union type in Julia, which is clearer to me after your explanation.

Best,
Theo

Stefan Karpinski

unread,
Mar 22, 2013, 3:33:06 PM3/22/13
to julia...@googlegroups.com
We are not very much in the C++ spirit in general. I haven't found tacking fields onto existing composite types to be a very useful thing and allowing it has lots of nasty side-effects. I'm pretty sure I've written about this subject before on julia-dev but I can't search for it right now since I'm on my phone. I'll see if I can find it later of someone else hasn't already dug it up.

Theodore Papamarkou

unread,
Mar 22, 2013, 3:49:34 PM3/22/13
to julia...@googlegroups.com
Thanks Stefan; are you referring to your comments that I found on the following julia-dev post?


I can see Julia's approach, and I agree that class-based OO is not a necessity (I am just used to it due to having programmed in C++ the last two years). However, I am already feeling comfortable with Julia's design based on composite types...

Stefan Karpinski

unread,
Mar 22, 2013, 4:23:37 PM3/22/13
to Julia Users
Yes, that's it, in particular this message.
Reply all
Reply to author
Forward
0 new messages