# Is there a way to block type conversion?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302>\
**Category:** General Usage\
**Created:** [November 16, 2023, 8:51am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302 "2023-11-16T08:51:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pao/32/34395_2.png) [@pao](https://discourse.julialang.org/u/pao)\
**Post date:** [November 16, 2023, 8:51am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302/1 "2023-11-16T08:51:44Z")

</div>

I’m writing some simple functions with combinatorial flavor (derived from IBM’s research language FL), but they cannot be used as needed by the intrusive type conversion.

Example:

```julia
julia> DISTL([100,[10.,20.,30.]]) # distribute left
3-element Vector{Vector{Float64}}:
 [100.0, 10.0]
 [100.0, 20.0]
 [100.0, 30.0]

```

while I need

```julia
julia> DISTL([100,[10.,20.,30.]])
3-element Vector{Vector{Any}}:
 [100, 10.0]
 [100, 20.0]
 [100, 30.0]

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 16, 2023, 9:01am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302/2 "2023-11-16T09:01:42Z")

</div>

> [@pao](#):
>
> `[100,[10.,20.,30.]]`

Could you share with us your mplementation of `DISTL`?

Julia itself does not appear to be doing type conversion of the input.

```julia-repl
julia> [100,[10.,20.,30.]]
2-element Vector{Any}:
 100
    [10.0, 20.0, 30.0]

```

I might implement DISTL as follows to get a similar result.

```julia-repl
julia> DISTL(input) = vcat.(input[1], input[2])
DISTL (generic function with 1 method)

julia> DISTL([100, [10., 20., 30.]]) 3-element Vector{Vector{Float64}}:
 [100.0, 10.0]
 [100.0, 20.0]
 [100.0, 30.0]

```

Fixing it would look as follows.

```julia
julia> DISTL(input) = Base.typed_vcat.((Number,), input[1], input[2])
DISTL (generic function with 1 method)

julia> DISTL([100, [10., 20., 30.]])          
3-element Vector{Vector{Number}}:
 [100, 10.0]
 [100, 20.0]
 [100, 30.0]

```

---

<div class="post-metadata">

**Author:** ![pao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pao/32/34395_2.png) [@pao](https://discourse.julialang.org/u/pao)\
**Post date:** [November 16, 2023, 9:05am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302/3 "2023-11-16T09:05:00Z")

</div>

> [@mkitti](#):
>
> Could you share with us your mplementation of `DISTL`?

```julia
function DISTL(args)
	Element, List = args
	return [[Element, e] for e in List]
end

```

---

<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:** [November 16, 2023, 9:06am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302/4 "2023-11-16T09:06:03Z")

</div>

How about specifying the element type you want?

```julia
function DISTL(args)
	Element, List = args
	return [Any[Element, e] for e in List]
end

```

You don’t have to specify a known type, you can do it generically, even make it an argument

```julia
function DISTL(args, T) # 2nd method
	Element, List = args
	return [T[Element, e] for e in List]
end

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 16, 2023, 9:07am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302/5 "2023-11-16T09:07:25Z")

</div>

I was going to suggest

```julia
julia> function DISTL(args)
           Element, List = args
           return [Number[Element, e] for e in List]
       end
DISTL (generic function with 1 method)

julia> DISTL([100, [10., 20., 30.]])
3-element Vector{Vector{Number}}:
 [100, 10.0]                                 
 [100, 20.0]
 [100, 30.0]

```

Here’s another version that creates a type Union.

```julia
julia> function DISTL(args)
           Element, List = args
           T = Union{typeof(Element), eltype(List)}
           return [T[Element, e] for e in List]
       end
DISTL (generic function with 1 method)

julia> DISTL([100, [10., 20., 30.]])
3-element Vector{Vector{Union{Float64, Int64}}}:                               
 [100, 10.0]
 [100, 20.0]                                 
 [100, 30.0]

```

---

<div class="post-metadata">

**Author:** ![pao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pao/32/34395_2.png) [@pao](https://discourse.julialang.org/u/pao)\
**Post date:** [November 16, 2023, 9:10am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302/6 "2023-11-16T09:10:28Z")

</div>

> [@Benny](#):
>
> ```julia
> function DISTLtyped(args, T)
> Element, List = args
> return [T[Element, e] for e in List]
> end
> 
> ```

This is OK, because one needed characteristic is to work with “any” kind of object  
Thank you !!

---

<div class="post-metadata">

**Author:** ![pao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pao/32/34395_2.png) [@pao](https://discourse.julialang.org/u/pao)\
**Post date:** [November 16, 2023, 9:16am UTC](https://discourse.julialang.org/t/is-there-a-way-to-block-type-conversion/106302/7 "2023-11-16T09:16:26Z")

</div>

> [@mkitti](#):
>
> ```julia
> julia> function DISTL(args)
> Element, List = args
> T = Union{typeof(Element), eltype(List)}
> return [T[Element, e] for e in List]
> end
> DISTL (generic function with 1 method)
> 
> julia> DISTL([100, [10., 20., 30.]])
> 3-element Vector{Vector{Union{Float64, Int64}}}:                               
> [100, 10.0]
> [100, 20.0]                                 
> [100, 30.0]
> 
> ```

THIS looks very OK. Thank you !!

```julia
julia> DISTL(["100", [10., 20., 30.]])
3-element Vector{Vector{Union{Float64, String}}}:
 ["100", 10.0]
 ["100", 20.0]
 ["100", 30.0]

```
