안녕하세요 여러분,
저는 작은 swatch 에디터를 만들고 있는데, 세 가지 case로 된 palette union이 있어요. 새로 편집을 시작하면 NotEntered, 의도적으로 비운 경우는 Empty, 그리고 Colors는 리스트를 가져요. case들을 private으로 만들고 ofColors만 공개했고, ofColors []는 Empty를 돌려줘요. 저는 컴파일러가 Colors []를 만들 수 없게 해 줄 거라고 생각했어요.
스크립트로 돌린 세션(새로 시작, blue 추가, blue 제거, Save)에서 Save가 "empty (chosen)" 대신 "0 colors"를 출력해요. Reset은 제대로 동작해요. Rider에도 밑줄이 안 뜨고 빌드도 깨끗해요.
처음엔 List.filter를 의심했고, 그다음엔 Rider가 global.json과 다른 SDK를 쓰는 건가 싶어서 터미널에서 다시 빌드해 봤어요. 결과는 똑같았어요.
원인이라고 생각하는 함수는 이거예요:
let remove (name: string) (p: Palette) =
match p with
| Colors cs -> Colors(cs |> List.filter (fun c -> c.Name <> name))
| other -> other
혹시 private은 Palette.fs 밖에서만 검사돼서, 같은 모듈 안에서는 Colors []를 마음대로 만들 수 있는 건가요? access control page를 보면 그런 것 같기도 한데 확신은 없어요. 그렇다면 add와 remove도 ofColors를 거치게 하는 게 보통의 해결 방법인가요, 아니면 타입 자체가 잘못된 건가요? 그냥 리스트 하나와 entered 플래그를 쓰는 게 더 간단할지도 궁금해요.
감사합니다,
Oskar
영어에서 번역·원문 보기Hello all,
I have a small swatch editor with a three-case palette union. A fresh edit is NotEntered, a deliberate clear is Empty, and Colors holds a list. I made the cases private and exposed only ofColors, where ofColors [] gives Empty. My expectation was that the compiler would make Colors [] impossible to build.
In a scripted session (fresh, add blue, remove blue, Save), Save prints "0 colors" instead of "empty (chosen)". Reset behaves correctly. Rider shows no squiggle and the build is clean.
I suspected List.filter first, then Rider using a different SDK than global.json, so I rebuilt from the terminal. The result was the same.
This is the function I think is responsible:
let remove (name: string) (p: Palette) =
match p with
| Colors cs -> Colors(cs |> List.filter (fun c -> c.Name <> name))
| other -> other
Is the privacy only checked outside Palette.fs, so my own module can build Colors [] freely? The access control page seems to suggest so, but I am not certain. If so, is routing add and remove through ofColors the normal fix, or is the type itself wrong? I am also wondering whether a plain list plus an entered flag would be simpler here.
Thank you,
Oskar