设置两侧不同类型的比较

时间:2016-02-28 18:59:32

标签: pascal

以下代码取自Gnu Pascal测试代码,将在Free Pascal中很好地编译。我可以理解它是如何工作的。

field

但是,我对于设置兼容性以及您可以期待的规则有点好奇。例如:

var s1 : set of 0..255;
    s2 : set of 64..128;
    ok : boolean;

procedure p1;
begin
  if s1 = s2 then begin 
    writeln('failed1');
    ok := false;
  end;
end;

这会打印'b'。但是如果我有两个空集(program p; var a : set of 0..10; b : set of 20..100; s : integer; begin b := [20]; a := []; if a = b then writeln('a') else writeln('b'); end. )那么它们被认为是相等的。

我只想弄清楚这实际上是如何实现的。

(我有点想的是,这两个集合被转换为范围的并集,因此创建了b := [];,并且set of 0..100a中有两个临时值b,然后比较那些临时工。)

1 个答案:

答案 0 :(得分:2)

将整数设置为基本类型(HWND mainWindow; WNDPROC defaultEditCallback = NULL; int WINAPI WinMain(HINSTANCE instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEXW wc; wchar_t windowClass[] = L"testcase"; wc.cbSize = sizeof(WNDCLASSEXW); wc.style = 0; wc.lpfnWndProc = mainWindowCallback; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = instance; wc.hIcon = NULL; wc.hCursor = LoadCursorW(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); wc.lpszMenuName = NULL; wc.lpszClassName = (LPCWSTR)windowClass; wc.hIconSm = NULL; RegisterClassExW(&wc); mainWindow = CreateWindowW( (LPCWSTR)windowClass, (LPCWSTR)windowClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 200, NULL, NULL, instance, NULL ); HWND edit = CreateWindowExW( WS_EX_CLIENTEDGE, (LPCWSTR)L"edit", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | WS_TABSTOP, 0, 0, 0, 0, mainWindow, (HMENU) 10, (HINSTANCE) GetWindowLongPtrW(mainWindow, GWLP_HINSTANCE), NULL ); defaultEditCallback = (WNDPROC)SetWindowLongPtrW(edit, GWLP_WNDPROC, (LONG)editCallback); HWND firstButton = createButton(mainWindow, 20, L"First", buttonWidth, buttonHeight); HWND secondButton = createButton(mainWindow, 30, L"Second", buttonWidth, buttonHeight); HWND thirdButton = createButton(mainWindow, 40, L"Third", buttonWidth, buttonHeight); // [Skipped] Sizing and positioning controls. ShowWindow(mainWindow, nCmdShow); UpdateWindow(mainWindow); MSG msg; while (GetMessageW(&msg, NULL, 0, 0) > 0) { if (!IsDialogMessage(mainWindow, &msg)) { TranslateMessage(&msg); DispatchMessageW(&msg); } } return (int)msg.wParam; } LRESULT CALLBACK mainWindowCallback(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) { if (WM_DESTROY == msg) { PostQuitMessage(0); } return DefWindowProcW(window, msg, wParam, lParam); } LRESULT CALLBACK editCallback(HWND control, UINT msg, WPARAM wParam, LPARAM lParam) { if (WM_KEYDOWN == msg && VK_TAB == wParam) { HWND next = GetNextDlgTabItem(mainWindow, control, (int)(GetKeyState(VK_SHIFT) & 0x8000)); SetFocus(next); return 0; } return CallWindowProc(defaultEditCallback, control, msg, wParam, lParam); } HWND createButton(HWND parentWindow, int id, wchar_t* caption, int width, int height) { return CreateWindowW( (LPCWSTR)L"button", (LPCWSTR)caption, WS_VISIBLE | WS_CHILD | WS_TABSTOP, 0, 0, width, height, parentWindow, (HMENU)id, NULL, NULL ); } 之后的类型)始终兼容。

您的假设是正确的:通常情况下,两组都转换为具有共同范围的临时值,然后比较临时值。通过调用set of来完成转换。

如果您对更多细节感兴趣,那么决定如何转换集合的代码位于nadd.pas(请参阅1),从第1593行开始。