---
title: "cast(to:)"
framework: coreml
role: symbol
role_heading: Instance Method
path: "coreml/mltensor/cast(to:)"
---

# cast(to:)

Casts the elements of the tensor to the given scalar type.

## Declaration

```swift
func cast<Scalar>(to scalarType: Scalar.Type) -> MLTensor where Scalar : MLTensorScalar
```

## Parameters

- `scalarType`: The destination scalar type.

## Return Value

Return Value A new tensor with its contents cast to the given scalar type.

## Discussion

Discussion For example: let x = MLTensor([1, 2, 3], scalarType: Int32.self) let y = x.cast(to: Float.self) y.scalarType // is Float

## See Also

### Casting the elements

- [cast(like:)](coreml/mltensor/cast(like:).md)
