# Creating AbstractStrings

**URL:** https://discourse.julialang.org/t/creating-abstractstrings/1982
**Category:** General Usage
**Tags:** question
**Created:** [February 8, 2017, 11:30am UTC](https://discourse.julialang.org/t/creating-abstractstrings/1982 "2017-02-08T11:30:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)
#### Post date: [February 8, 2017, 11:30am UTC](https://discourse.julialang.org/t/creating-abstractstrings/1982/1 "2017-02-08T11:30:51Z")

</div>

Hello,

I face a problem when creating an AbstractString type. Maybe is not the best implementation. Nevertheless, I want to give it a try. Here is my toy-model which fails of course, because, it is not finished. I don’t know how to continue with it.

```julia
bitstype 32 MyChar

type MyString <: AbstractString
  len::Int
  MyString(arr::Array{MyChar,1}) = begin
    # What should be go here?
    x = new(length(arr))
  end
end

# I'm only guessing
Base.endof(mystr::MyString) = mystr.len
Base.next(mystr::MyString, i::Int) = unsafe_load(pointer_from_objref(mystr), i+1)

x = reinterpret(MyChar, 0x00000000)
y = reinterpret(MyChar, 0x00000001)

mystr = MyString([x,y])

```

Thanks

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 8, 2017, 1:07pm UTC](https://discourse.julialang.org/t/creating-abstractstrings/1982/2 "2017-02-08T13:07:01Z")

</div>

You need a field to actually store the array in the type. And you don’t need a length field if you have an array field, since the length is stored in the array. And then you don’t need to declare a constructor, since the default will work.

```julia
type MyString <: AbstractString
  data::Vector{MyChar}
end
Base.endof(mystr::MyString) = length(mystr.data)
Base.next(mystr::MyString, i) = (mystr.data[i], i+1)
Base.start(mystr::MyString) = 1
Base.done(mystr::MyString, i) = i > length(mystr.data)

```

If you want a string type with 32-bit code units, then maybe you should just use the `UTF32String` type from [https://github.com/JuliaArchive/LegacyStrings.jl](https://github.com/JuliaArchive/LegacyStrings.jl)

---

<div class="post-metadata">

### Author: ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)
#### Post date: [February 11, 2017, 12:24am UTC](https://discourse.julialang.org/t/creating-abstractstrings/1982/3 "2017-02-11T00:24:22Z")

</div>

Thanks @stevengj!!
