# How can we maintain easy generics in the face of demand for a “safe” language?

**URL:** <https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734>\
**Category:** Internals & Design\
**Created:** [February 5, 2024, 2:11pm UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734 "2024-02-05T14:11:46Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [February 6, 2024, 7:27pm UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/21 "2024-02-06T19:27:14Z")

</div>

I’m not sure why we don’t have (as far as I’m aware) an `AbstractArrayTests` package that contains a full test suite you can run against an instantiated custom type that wants to be an abstract array. It could have flags for sub-interfaces, like “read-only array”.

Similarly for any other large interface.

[I’ve written such test suites for interfaces I’ve defined in my own framework](https://juliaquantumcontrol.github.io/QuantumControl.jl/dev/api/quantum_propagators/#QuantumPropagators.Interfaces.check_generator). They’re quite a bit of work to get right, but once you have them, I find them extremely useful.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 6, 2024, 9:00pm UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/22 "2024-02-06T21:00:55Z")

</div>

6 posts were split to a new topic: [Semantics of :: in return type vs. argument type annotations](https://discourse.julialang.org/t/semantics-of-in-return-type-vs-argument-type-annotations/109838)

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [February 7, 2024, 3:46am UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/26 "2024-02-07T03:46:07Z")

</div>

I like [SimpleTraits.jl](https://github.com/mauro3/SimpleTraits.jl) as a solution to this problem. Wish it could be done in base Julia eventually.

Since `hasmethod` is static on Julia 1.10, you could declare it as a trait like

```julia
hasprint(::Type{T}) where T = hasmethod(print, Tuple{T})

```

And then `hasprint(T)` defines your trait in the form of

```julia
@traitimpl HasPrint{T} <- hasprint(T)

```

Of course this doesn’t work for `print` because everything has a method, but you get the idea.

Then the function signature becomes

```julia
@traitfn function f(x::X) where {X; HasPrint{X}}

```

I wish we didn’t need a macro for this type of behavior though. But it gets the job done for now.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [February 7, 2024, 3:45pm UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/27 "2024-02-07T15:45:49Z")

</div>

Actually [WhereTraits.jl](https://github.com/jolin-io/WhereTraits.jl) looks even better for this. You can literally just do

```julia
@traits f(x) where {applicable(print, x)} = ...

```

and it will dispatch depending on that boolean condition.

However the downside is this is not zero-overhead whereas SimpleTraits.jl is.

(Although WhereTraits allows multiple traits; SimpleTraits only permits one)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 7, 2024, 3:51pm UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/28 "2024-02-07T15:51:35Z")

</div>

> [@NicholasWMRitchie](#):
>
> it is hard to know when you have satisfied enough of the informal interface to be a plug in replacement for the canonical implementation

IMO the best solution for these problems is providing a **test suite** for a particular interface. The implementor plugs in the type or a value, and the test suite reports detailed results or a pass/fail.

This

1. would not burden the language with extra syntax and complexity

2. allow the users who implement an interface to check the **semantics** , and not just a formal presence of method signatures.

Eg I can implement the array interface and include

```julia
Base.firstindex(v::MyVector) = length(v)
Base.lastindex(v::MyVector) = 1

```

which does IMO is not something a formal interface spec would catch. But a test suite could. Of course in due time the test suite could be enhanced by tests for errors that slip though.

---

<div class="post-metadata">

**Author:** ![NicholasWMRitchie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicholaswmritchie/32/22449_2.png) [@NicholasWMRitchie](https://discourse.julialang.org/u/NicholasWMRitchie)\
**Post date:** [February 7, 2024, 5:34pm UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/29 "2024-02-07T17:34:21Z")

</div>

I can see the value of test suites. As you say, they go beyond simply having the correct methods to ensuring that the methods are implemented in a suitable manner.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [February 7, 2024, 6:01pm UTC](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/30 "2024-02-07T18:01:05Z")

</div>

Test suites are great, just, [as I was saying before](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734/21), they’re a little tedious to write and require discipline to use. Ideally, I’d like to see both explicit verification of interfaces via testing _and_ better static code analysis. But any non-trivial interface should 100% have an associated set of tests.

[Previous page](https://discourse.julialang.org/t/how-can-we-maintain-easy-generics-in-the-face-of-demand-for-a-safe-language/109734.md?page=1)
