Skip to content

Commit db2ba9e

Browse files
cartermpKevinRansom
authored andcommitted
[RFC 1082] - uint type abbreviation (#8185)
* Add uint type abbreviation * Add uint casting function and test * Update surface area tests * update
1 parent d3d5e30 commit db2ba9e

File tree

9 files changed

+36
-1
lines changed

9 files changed

+36
-1
lines changed

src/fsharp/FSharp.Core/prim-types-prelude.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace Microsoft.FSharp.Core
2828
type bool = System.Boolean
2929
type decimal = System.Decimal
3030
type int = int32
31+
type uint = uint32
3132

3233
type ``[]``<'T> = (# "!0[]" #)
3334
type ``[,]``<'T> = (# "!0[0 ...,0 ...]" #)

src/fsharp/FSharp.Core/prim-types-prelude.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ namespace Microsoft.FSharp.Core
7878
/// <summary>An abbreviation for the CLI type <c>System.Int32</c>.</summary>
7979
type int = int32
8080

81+
/// <summary>An abbreviation for the CLI type <c>System.UInt32</c>.</summary>
82+
type uint = uint32
83+
8184
/// <summary>Single dimensional, zero-based arrays, written <c>int[]</c>, <c>string[]</c> etc.</summary>
8285
/// <remarks>Use the values in the <c>Array</c> module to manipulate values
8386
/// of this type, or the notation <c>arr.[x]</c> to get/set array

src/fsharp/FSharp.Core/prim-types.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3731,7 +3731,10 @@ namespace Microsoft.FSharp.Core
37313731
when ^T : byte = (# "conv.i4" value : int32 #)
37323732

37333733
[<CompiledName("ToInt")>]
3734-
let inline int value = int32 value
3734+
let inline int value = int32 value
3735+
3736+
[<CompiledName("ToUInt")>]
3737+
let inline uint value = uint32 value
37353738

37363739
[<CompiledName("ToEnum")>]
37373740
let inline enum< ^T when ^T : enum<int32> > (value:int32) : ^T = EnumOfValue value

src/fsharp/FSharp.Core/prim-types.fsi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,15 @@ namespace Microsoft.FSharp.Core
26392639
[<CompiledName("ToInt")>]
26402640
val inline int : value:^T -> int when ^T : (static member op_Explicit : ^T -> int) and default ^T : int
26412641

2642+
/// <summary>Converts the argument to an unsigned 32-bit integer. This is a direct conversion for all
2643+
/// primitive numeric types. For strings, the input is converted using <c>UInt32.Parse()</c>
2644+
/// with InvariantCulture settings. Otherwise the operation requires an appropriate
2645+
/// static conversion method on the input type.</summary>
2646+
/// <param name="value">The input value.</param>
2647+
/// <returns>The converted int</returns>
2648+
[<CompiledName("ToUInt")>]
2649+
val inline uint: value:^T -> uint when ^T: (static member op_Explicit: ^T -> uint) and default ^T: uint
2650+
26422651
/// <summary>Converts the argument to a particular enum type.</summary>
26432652
/// <param name="value">The input value.</param>
26442653
/// <returns>The converted enum type.</returns>

tests/FSharp.Core.UnitTests/FSharp.Core/PrimTypes.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ type m
1616
[<TestFixture>]
1717
type LanguagePrimitivesModule() =
1818

19+
[<Test>]
20+
member _.CastingUint () =
21+
let expected = 12u
22+
let actual = uint 12
23+
Assert.AreEqual(expected, actual)
24+
1925
[<Test>]
2026
member this.CastingUnits() =
2127
let f = 2.5

tests/FSharp.Core.UnitTests/SurfaceArea.coreclr.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,7 @@ Microsoft.FSharp.Core.Operators: Int32 Sign[T](T)
20772077
Microsoft.FSharp.Core.Operators: Int32 SizeOf[T]()
20782078
Microsoft.FSharp.Core.Operators: Int32 ToInt32[T](T)
20792079
Microsoft.FSharp.Core.Operators: Int32 ToInt[T](T)
2080+
Microsoft.FSharp.Core.Operators: UInt32 ToUInt[T](T)
20802081
Microsoft.FSharp.Core.Operators: Int32 limitedHash[T](Int32, T)
20812082
Microsoft.FSharp.Core.Operators: Int64 ToInt64[T](T)
20822083
Microsoft.FSharp.Core.Operators: IntPtr ToIntPtr[T](T)

tests/FSharp.Core.UnitTests/SurfaceArea.net40.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,7 @@ Microsoft.FSharp.Core.Operators: Int32 Sign[T](T)
20772077
Microsoft.FSharp.Core.Operators: Int32 SizeOf[T]()
20782078
Microsoft.FSharp.Core.Operators: Int32 ToInt32[T](T)
20792079
Microsoft.FSharp.Core.Operators: Int32 ToInt[T](T)
2080+
Microsoft.FSharp.Core.Operators: UInt32 ToUInt[T](T)
20802081
Microsoft.FSharp.Core.Operators: Int32 limitedHash[T](Int32, T)
20812082
Microsoft.FSharp.Core.Operators: Int64 ToInt64[T](T)
20822083
Microsoft.FSharp.Core.Operators: IntPtr ToIntPtr[T](T)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace FSharp.Compiler.UnitTests
2+
3+
open NUnit.Framework
4+
5+
[<TestFixture>]
6+
module UIntTests =
7+
let ``uint type abbreviation works`` () =
8+
let src = "let x = uint 12"
9+
let expectedErrors = [||]
10+
CompilerAssert.TypeCheckWithErrors src expectedErrors

tests/fsharp/FSharpSuite.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Compile Include="Compiler\Warnings\ExperimentalAttributeTests.fs" />
6060
<Compile Include="Compiler\Warnings\PatternMatchingWarningTests.fs" />
6161
<Compile Include="Compiler\SourceTextTests.fs" />
62+
<Compile Include="Compiler\Language\UIntTests.fs" />
6263
<Compile Include="Compiler\Language\FixedIndexSliceTests.fs" />
6364
<Compile Include="Compiler\Language\SlicingQuotationTests.fs" />
6465
<Compile Include="Compiler\Language\CustomCollectionTests.fs" />

0 commit comments

Comments
 (0)