# Help with my string type

**URL:** https://discourse.julialang.org/t/help-with-my-string-type/10743
**Category:** New to Julia
**Created:** [May 6, 2018, 4:56pm UTC](https://discourse.julialang.org/t/help-with-my-string-type/10743 "2018-05-06T16:56:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [May 6, 2018, 4:56pm UTC](https://discourse.julialang.org/t/help-with-my-string-type/10743/1 "2018-05-06T16:56:52Z")

</div>

Hi there,

I’m trying to implement a new string type consisting only of the characters [A-Z] and [0-9]:

```julia
abstract type AbstractMyString <: AbstractString end

struct MyString <: AbstractMyString
    str::String
    MyString(str) = begin
        ismatch(CSE, str) || error("string contains illegal characters")
        return new(str)
    end
end

const CSE = r"^.([A-Z]|[0-9])+$"

```

The problem I have is that I want `MyString` to work in functions that expect `String`. For example, I get the following error:

```julia
a = MyString("ASD1")
"Error showing value of type MyString:
ERROR: you must implement endof(MyString)

```

Which can be remedied by preventing the REPL from trying to print `a`, using a semi colon.

I thought that I could declare everything to be a subtype of `AbstractString` (as I cannot declare it to be subtype of `String`) and expect that everything that works with `AbstractString` to work with `MyString`. Where did I go wrong? How can I inherit the things that work with `AbstractString`?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 6, 2018, 5:00pm UTC](https://discourse.julialang.org/t/help-with-my-string-type/10743/2 "2018-05-06T17:00:46Z")

</div>

You can defined `Base.endof(str::MyString) = endof(str.str)`.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [May 6, 2018, 5:06pm UTC](https://discourse.julialang.org/t/help-with-my-string-type/10743/3 "2018-05-06T17:06:18Z")

</div>

Yes, of course. But then I also have to define `next(str::MyString)`, `next(str::MyString, ::Int)`, etc. I thought I could save myself that.

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [May 6, 2018, 5:08pm UTC](https://discourse.julialang.org/t/help-with-my-string-type/10743/4 "2018-05-06T17:08:15Z")

</div>

`AbstractString` is an _interface_; meaning if you implement a few select methods, _then_ you automatically hook into a bunch of methods defined on AbstractStrings. Granted, _interfaces_ aren’t a totally concrete/fleshed out concept in julia; for example, it’d be nice if when you subtyped `AbstractString`, but didn’t implement the required methods, there was an immediate error somehow indicating the methods needed to fulfill the interface.

Of interest to you is that the AbstractString interface has actually changed between 0.6 and 0.7, with, IMO, 0.7 having a much cleaner interface and definitely better docs to explain everything. Take a look [here](https://github.com/JuliaLang/julia/blob/master/base/strings/basic.jl) to get a better idea of what’s expected of `AbstractString` subtypes.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [May 6, 2018, 11:16pm UTC](https://discourse.julialang.org/t/help-with-my-string-type/10743/5 "2018-05-06T23:16:47Z")

</div>

Also see discussions about inheriting interfaces:

> [@How do you inherit Interface?](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/2):
>
> There is no generic built-in mechanism for this. For the special (and quite common) case when you want the container to “inherit” functionality from an element, some packages have macros that implement this, eg [Lazy.@forward](https://github.com/MikeInnes/Lazy.jl/blob/76dab7d30863e8375fc96b2444fdb2ead369bf4c/src/macros.jl#L248-L271).

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [May 7, 2018, 3:40am UTC](https://discourse.julialang.org/t/help-with-my-string-type/10743/6 "2018-05-07T03:40:33Z")

</div>

Thanks, this helps a lot. However, I’d still need to know what needs forwarding.
