Skip to content

Instantly share code, notes, and snippets.

@matarillo
Forked from ufcpp/NeitherTrueNorFalse.cs
Last active May 26, 2016 10:05
Show Gist options
  • Save matarillo/0b6815729dd20fb17c0ee62210dfeb3c to your computer and use it in GitHub Desktop.
Save matarillo/0b6815729dd20fb17c0ee62210dfeb3c to your computer and use it in GitHub Desktop.
false でも true でもない何か
using System;
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
Write(false);
Write(true);
Write(Bool(0)); // false と一緒
Write(Bool(1)); // true と一緒
Write(Bool(2)); // false でも true でもない何か
}
private static bool Bool(byte value)
{
var union = new MyUnion();
union.Byte = value;
return union.Bool;
}
[StructLayout(LayoutKind.Explicit)]
private struct MyUnion
{
[FieldOffset(0)]
public bool Bool;
[FieldOffset(0)]
public byte Byte;
}
static void Write(bool x)
{
switch (x)
{
case true:
Console.WriteLine("True");
break;
case false:
Console.WriteLine("False");
break;
default:
Console.WriteLine("Other ");
break;
}
}
}
#nowarn "9"
open System.Runtime.InteropServices
[<StructLayout(LayoutKind.Explicit)>]
type MyUnion =
struct
[<FieldOffset(0)>]
val mutable bool: bool
[<FieldOffset(0)>]
val mutable byte: byte
end
let bool value =
let mutable union = new MyUnion()
union.byte <- value
union.bool
let write = function
| false -> printfn "%s" "False"
| true -> printfn "%s" "True"
| _ -> printfn "%s" "Other"
[<EntryPoint>]
let main argv =
write true
write false
write (bool 0uy)
write (bool 1uy)
write (bool 2uy)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment