# What about an \`undefs\` function in \`Base\`?

**URL:** https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690
**Category:** General Usage
**Tags:** feature-request
**Created:** [January 9, 2023, 2:18am UTC](https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690 "2023-01-09T02:18:50Z")
**Posts on this page:** 5
**Page:** 4

<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: [January 20, 2023, 3:15am UTC](https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690/61 "2023-01-20T03:15:24Z")

</div>

Perhaps there should be a `@CuArray` macro if there is not one already. StaticArrays.jl has a `@SArray` macro.

```julia
julia> @SArray [x for x in 1:5]
5-element SVector{5, Int64} with indices SOneTo(5):
 1
 2
 3
 4
 5

```

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 20, 2023, 3:58am UTC](https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690/62 "2023-01-20T03:58:40Z")

</div>

Funny that `SA` doesn’t work with comprehensions.

```julia
julia> SA[1, 2, 3]
3-element SVector{3, Int64} with indices SOneTo(3):
 1
 2
 3

julia> SA[x for x=1:3]
ERROR: MethodError: Cannot `convert` an object of type
  Int64 to an object of type
  SA

```

So for comprehensions, you have to use the macro? Sidenote, the space between the macrocall and the brackets is optional:

```julia
julia> SArray[@SArray[1,2,3] for _=1:4]
4-element Vector{SArray}:
 [1, 2, 3]
 [1, 2, 3]
 [1, 2, 3]
 [1, 2, 3]

julia> @SArray[@SArray[1,2,3] for _=1:4]
4-element SVector{4, SVector{3, Int64}} with indices SOneTo(4):
 [1, 2, 3]
 [1, 2, 3]
 [1, 2, 3]
 [1, 2, 3]

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [January 20, 2023, 8:03am UTC](https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690/63 "2023-01-20T08:03:39Z")

</div>

> [@uniment](#):
>
> Is there a syntax for array comprehensions of different array types, such as `CuArray`? Maybe such a thing would diminish the need for pre-allocation and `for` loops to begin with. Something like this, perhaps?
> 
> ```julia
> Type{CuArray{Int}}[i*j for i=1:2, j=1:2]
> 
> ```

I think custom array types can define constructors that accept generators, e.g.

```julia
CuArray(Int, (i*j for i=1:2, j=1:2))

```

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 20, 2023, 8:13am UTC](https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690/64 "2023-01-20T08:13:02Z")

</div>

Should that be part of `AbstractArray` interface?

```julia
CuArray{Int}(i*j for i=1:2, j=1:2)

```

---

<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: [January 24, 2023, 10:58am UTC](https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690/65 "2023-01-24T10:58:38Z")

</div>

Undefs.jl is now registered:

> [@Undefs.jl: Convenience and Experiment](https://discourse.julialang.org/t/undefs-jl-convenience-and-experiment/93451):
>
> Undefs.jl I created a registered package called Undefs.jl that exports three names: undefs(A = Array, T = Float64, dims) = A{T}(undef, dims): a convenience method similar to ones, zeros, trues, and falses. undef!(array::Array, index=1): An experimental method to reset an element to an #undef state. This also works on Base.RefValue. @undef! A[1]: A macro to make the use of the undef! easier. Demonstration of the convenience constructor, undefs julia\> using Undefs julia\> mutable struct Foo …

[Previous page](https://discourse.julialang.org/t/what-about-an-undefs-function-in-base/92690.md?page=3)
