# Generic function to double a value

**URL:** <https://forum.modular.com/t/generic-function-to-double-a-value/2193>\
**Category:** Mojo\
**Created:** [August 27, 2025, 7:58am UTC](https://forum.modular.com/t/generic-function-to-double-a-value/2193 "2025-08-27T07:58:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius.d](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@marius.d](https://forum.modular.com/u/marius.d)\
**Post date:** [August 27, 2025, 7:58am UTC](https://forum.modular.com/t/generic-function-to-double-a-value/2193/1 "2025-08-27T07:58:03Z")

</div>

See the following functions. `dbl2` works with SIMD inputs, but `dbl` does not compile: `error: expected a type, not a value`.  
How can I implement this generic dbl without using SIMD types?

```mojo
fn dbl[T: DType](x: T) -> T:
    return 2*x

fn dbl2[T: DType](x: Scalar[T]) -> Scalar[T]:
    return 2*x

fn main():
    var x = 4
    var y = dbl(x)
    print(y)

    var x2 = SIMD[DType.float32, 1](4)
    var y2 = dbl2(x2)
    print(y2)

```

---

<div class="post-metadata">

**Author:** ![Ehsan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ehsan/32/142_2.png) [@Ehsan](https://forum.modular.com/u/Ehsan)\
**Post date:** [August 28, 2025, 3:48pm UTC](https://forum.modular.com/t/generic-function-to-double-a-value/2193/2 "2025-08-28T15:48:02Z")

</div>

Numerical values in Mojo are SIMD based that means `DType` by its own needs to be wrapped in `SIMD`. The `Scalar[T]` is `SIMD[T, 1]`where `1` is the SIMD width. For convenience there’re SIMD aliases for each DType like `Float32` which corresponds to `DType.float32`.

---

<div class="post-metadata">

**Author:** ![marius.d](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@marius.d](https://forum.modular.com/u/marius.d)\
**Post date:** [August 28, 2025, 6:58pm UTC](https://forum.modular.com/t/generic-function-to-double-a-value/2193/3 "2025-08-28T18:58:27Z")

</div>

If we do `var x = 4`, the type of x is `Int`. The `dbl2(x)` does not work in this case. However, if I do `var x = Int32(4)`, `dbl2(x)` works.  
`Int`being used by default and not being one of the `Int32, Int16 etc` looks strange to me. It can create inconsistencies, and the example here is one such case.

---

<div class="post-metadata">

**Author:** ![marius.d](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@marius.d](https://forum.modular.com/u/marius.d)\
**Post date:** [August 28, 2025, 7:27pm UTC](https://forum.modular.com/t/generic-function-to-double-a-value/2193/4 "2025-08-28T19:27:18Z")

</div>

I want to simplify this function a bit more and I was thinking about having something like this:

`fn dbl[dT: DType, T: Scalar[dT](x: T) -> T:`

My intent is to create a type `T` based on `dT`.  
`T` however is seen as a value of type `Scalar[dT]`and therefore this exact code does not work.

Is there a way to make `T`a type?

---

<div class="post-metadata">

**Author:** ![Ehsan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ehsan/32/142_2.png) [@Ehsan](https://forum.modular.com/u/Ehsan)\
**Post date:** [August 29, 2025, 12:47am UTC](https://forum.modular.com/t/generic-function-to-double-a-value/2193/5 "2025-08-29T00:47:54Z")

</div>

Correct, should’ve mentioned `Int` is different and is like `usize` but can be converted to `Int32` or `Int64`.

> I want to simplify this function a bit more and I was thinking about having something like this:
> 
> `fn dbl[dT: DType, T: Scalar[dT](x: T) -> T:`

I think what you what is perhaps something like this? Note that `param` is compile-time (see `alias` in `main`) and arg is runtime value:

```auto
fn foo[dtype: DType, param: Scalar[dtype]](arg: Scalar[dtype]) -> Scalar[dtype]:
    return param * arg

fn main():
    alias param = 2
    x= foo[DType.int32, param](Int32(2))
    print(x)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [February 25, 2026, 12:48am UTC](https://forum.modular.com/t/generic-function-to-double-a-value/2193/6 "2026-02-25T00:48:27Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
