Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Font immutable?

Font being immutable distresses both the programmer and the GC, because you need to create a new instance every time.

Why is Font an immutable reference type then?

like image 873
Letterman Avatar asked Oct 13 '09 15:10

Letterman


Video Answer


2 Answers

It simplifies the usage from the render system.

If the framework were to allow Font to be mutable, it would need to detect changes, and rework how it's rendering happens on a regular basis. Since Font creates a native resource, keeping this immutable prevents the system from worrying about having to recreate handles internally on a repeated basis.

Also, I disagree in terms of "Distress to the programmer". By making Font immutable, it makes it more obvious what is happening when the user creates a Font object. If you want a new Font, you need to create a new Font object, which in turn creates new native font resources. Making Font immutable makes it more clear what is happening - you're less likely to create a performance problem accidentally.

Were Font mutable, it would be less obvious that you were creating handles repeatedly as you changed your Font properties.

like image 149
Reed Copsey Avatar answered Oct 07 '22 12:10

Reed Copsey


Well, ask yourself some questions.

First, is a font logically a mutable thing, like a grocery list, or an immutable thing, like a number? If you are modelling a grocery list in a program, it makes sense to make it mutable because you typically think of having one grocery list the contents of which change as you run out of or purchase particular items. But numbers you typically model as being immutable -- the number 12 is the number 12, now and forever.

I think of "Helvetica 12 point bold" as being a fixed, immutable thing, like a number, not something I can change.

Second, is a font logically more like a value that you can make copies of, or is it more like a single thing that you can refer to? I don't think of having "two copies" of Helvetica; I think of referring to Helvetica. Whereas numbers I think of as having different copies of for different purposes -- when I have 12 items on my grocery list and 12 keys on my keyring, I don't think of both of those things as "referring to 12".

Since I think of fonts as being immutable and referred to, rather than as mutable and copied by value, I personally would model fonts as immutable reference types. Perhaps your intuitions about fonts are different than mine.

like image 39
Eric Lippert Avatar answered Oct 07 '22 12:10

Eric Lippert