[This (part A) is probably not implemented yet, and probably not even a new idea.. possibly julia-dev material?]

On mið 23.sep 2015 16:26, Milan Bouchet-Valat wrote:
Le mercredi 23 septembre 2015 à 07:38 -0700, Páll Haraldsson a écrit :

instead of these two type-unstable variants:
type MyType
     s::String
end

or of
type MyType
     s::Union(UTF8String,UTF16String)
end

A.
Are these (almost*) equivalent? For speed at least (generate same machine code)? [To each other, but not to having only one type - type stable.]

That may have been a misunderstanding of mine, because it doesn't have to be that way.


I know String (abstract) can have any number of concrete implementation, but at any point in time, just a few, and I know more than two (e.g. UTF32String).

With a Union or two (or any limited number of types) it seems what extra indirection (converting to if or "switch", and possibly borrowing bits in the pointers), might be avoided, by clever optimization, that I guess are not there - yet..


But in the former case and say there were only these two subtypes, it is not safe to do these optimizations? Because you can always add more subtypes at runtime.. Possibly until you actually do that, you could, but still then you would have to recompile code at that point..



B.
* I see the former is nicer, I was going to test with @code_native myself (but only on 0.3), but with the latter type:

b=MyType2("Palli")
ERROR: `convert` has no method matching convert(::Type{Union(UTF16String,UTF8String)}, ::ASCIIString)
 in MyType2 at no file

julia> b=MyType2(UTF8String("Palli"))
ERROR: `convert` has no method matching convert(::Type{Array{Uint8,1}}, ::ASCIIString)
you may have intended to import Base.convert
 in UTF8String at no file


I guess you just need a constructor. I also wander if that could be automatically generated for you..

--
Palli.

Reply via email to