# Auto-convert Vector{Type} to special container type

**URL:** <https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045>\
**Category:** General Usage\
**Tags:** package, type, arrays\
**Created:** [August 26, 2026, 3:22am UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045 "2026-08-26T03:22:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pekuntzpuglia](https://avatars.discourse-cdn.com/v4/letter/p/3d9bf3/32.png) [@pekuntzpuglia](https://discourse.julialang.org/u/pekuntzpuglia)\
**Post date:** [August 26, 2026, 3:22am UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045/1 "2026-08-26T03:22:59Z")

</div>

I’m trying to use this unofficial CasADi interface in Julia since the [official interface](https://github.com/casadi/LibCasADi.jl) seems to be a long way from working (couldn’t get it installed in Julia 1.10, as stated in Project.toml). I’m having a hard time handling the fact that CasADi’s main number type, `SX`, is supposed to behave both as a scalar and as an array, since this leads to vector expressions mixing `Array{SX}` and `SX`. As an example, the following code errors:

```
using CasADi
v = SX("v", 2)
[v[2]; v[1]] + v

```

because it actually tries to add a `Vector{SX}` to a `SX`, thought to be a scalar (since `SX <: Real`):

> ERROR: MethodError: no method matching +(::Vector{SX}, ::SX)  
> For element-wise addition, use broadcasting with dot syntax: array .+ scalar  
> The function `+` exists, but no method is defined for this combination of argument types.
> 
> Closest candidates are:  
> +(::Any, ::Any, ::Any, ::Any…)  
> @ Base operators.jl:642  
> +(::PyCall.PyObject, ::Any)  
> @ PyCall ~/.julia/packages/PyCall/1gn3u/src/pyoperators.jl:13  
> +(::Complex{Bool}, ::Real)  
> @ Base complex.jl:323  
> …
> 
> Stacktrace:  
> [1] top-level scope  
> @ REPL

So I wonder:

- can I hijack the `Array{SX}` constructor to always return an `SX`?
- how can I make this work with expressions like `scalar_sx * I(3)`, where the result is a `Diagonal <: AbstractMatrix`?
- can I use `promote_rule` for this?
- how could I overload the array building operator `[...]` to make it return an `SX` instead of an `Array`?

Currently, I’m handling this through a converter function inserted manually as-needed in my code:

```julia-auto
convertSX(x::AbstractArray{SX}) = SX(x)
convertSX(x::AbstractArray) = x

```

but this is cumbersome, unreliable, and I’m sure there’s a better way of doing this.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 26, 2026, 10:27am UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045/2 "2026-08-26T10:27:12Z")

</div>

> [@pekuntzpuglia](#):
>
> - can I hijack the `Array{SX}` constructor to always return an `SX`?

It’d contradict what the constructor is documented to do, and it wouldn’t affect your example anyway because the `N`-dimensional type was never called, the `1`-dimensional one was.

> [@pekuntzpuglia](#):
>
> - how could I overload the array building operator `[...]` to make it return an `SX` instead of an `Array`?

This also contradicts the documented behavior, and see `help?> []` for all the functions you’d have to extend.

My two cents, steer clear from hacking base Julia’s `Array` constructors and syntax because it’s designed for generic element types of `Array`, not generic array types. `SX(...)` is implemented and looks fairly simple. Your `convertSX` has a reasonable fallback to dodge what the `SX` call does to other arrays, but `convert(SX, ...)` is already there if you need it to error instead.

> [@pekuntzpuglia](#):
>
> - how can I make this work with expressions like `scalar_sx * I(3)`

What do you need to happen here, exactly? The wrapper does `SX(...)` conversion already.

> [@pekuntzpuglia](#):
>
> - can I use `promote_rule` for this?

Type promotion is based on a conventional ordering of nominally broadening types (broadening does not imply maintaining numeric precision). There’s no such convention for dense multidimensional arrays and CasADi types.

---

<div class="post-metadata">

**Author:** ![pekuntzpuglia](https://avatars.discourse-cdn.com/v4/letter/p/3d9bf3/32.png) [@pekuntzpuglia](https://discourse.julialang.org/u/pekuntzpuglia)\
**Post date:** [August 27, 2026, 2:12am UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045/3 "2026-08-27T02:12:55Z")

</div>

I would want `scalar_SX * I(3)` to return an `SX` expression, instead of a `Diagonal{...}`.

I gather this `convertSX` is the most practical approach to dodge this (rather unfortunate) decision of treating `SX` as a `Real`, which prevents a lot of convenient vector operations.

I noticed later that packages that introduce new vector types, such as StaticArrays, always introduce a different bracket operator, such as `SA[...]` due to the limitations you mentioned.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 27, 2026, 5:09am UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045/4 "2026-08-27T05:09:55Z")

</div>

> [@pekuntzpuglia](#):
>
> I would want `scalar_SX * I(3)` to return an `SX` expression, instead of a `Diagonal{...}`.

Looks like it does to me, do you have an example?

```julia-auto
julia> using CasADi, LinearAlgebra; typeof(SX("v") * I(3))
SX

```

> [@pekuntzpuglia](#):
>
> always introduce a different bracket operator, such as `SA[...]`

It’s not common in my experience, and it’s basic indexing syntax lowering to `getindex(SA, ...)`. The type (or singleton) `SA` serves to dispatch to a nominal `getindex` method that forwards to another type’s constructor. Base Julia uses this for setting explicit element types of `Array` e.g. `Int[1,2,3]`. `StaticArrays.SA` does this for instantiating `SVector`s with inferred element types e.g. `SA[1,2,3]` or explicit element types e.g. `SA{Int}[1,2.0,3]`. `SA` is itself an instantiable type e.g. `SA{Int}()`, but these instances aren’t intended to be used anywhere and justify `SA[...]` not instantiating `Array{SA}`. I don’t know how this would work across the `PythonCall` barrier and don’t expect it to help with most of the proposed type features here, but I’m just explaining this in case you’re interested in implementing a bracket operator for some convenience.

---

<div class="post-metadata">

**Author:** ![pekuntzpuglia](https://avatars.discourse-cdn.com/v4/letter/p/3d9bf3/32.png) [@pekuntzpuglia](https://discourse.julialang.org/u/pekuntzpuglia)\
**Post date:** [August 27, 2026, 12:41pm UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045/5 "2026-08-27T12:41:17Z")

</div>

Weird. In my machine:

```julia-auto
julia> using CasADi, LinearAlgebra; typeof(SX("x") * I(3))
Diagonal{SX, Vector{SX}}

```

I’m in Julia 1.12 using

> [c49709b8] CasADi v0.2.0 `git@github.com:ichatzinikolaidis/CasADi.jl.git#main`  
> [37e2e46d] LinearAlgebra v1.12.0

I would much rather have the behavior you’re encountering.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 27, 2026, 1:55pm UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045/6 "2026-08-27T13:55:57Z")

</div>

Ah, I’m using the SciML fork of CasADi in the General registry, v1.3.0. Reused UUID, so probably intended as a successor. Funnily enough, the fork’s v0.2.0 isn’t capable of `SX("x") * I(3)` at all; that’s how much it was reworked.

---

<div class="post-metadata">

**Author:** ![pekuntzpuglia](https://avatars.discourse-cdn.com/v4/letter/p/3d9bf3/32.png) [@pekuntzpuglia](https://discourse.julialang.org/u/pekuntzpuglia)\
**Post date:** [September 1, 2026, 12:36pm UTC](https://discourse.julialang.org/t/auto-convert-vector-type-to-special-container-type/139045/7 "2026-09-01T12:36:07Z")

</div>

Switching to SciML’s version does solve (partly) my issue. This and the `convertSX` approach allow me to code almost as I would without CasADi.
