Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContextAttribs and PixelFormat builder without unexpected behaviour #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gmacd
Copy link

@gmacd gmacd commented Nov 20, 2012

Edited after original change was questioned.

I've committed versions of PixelFormat and ContextAttribs that are both fully immutable, and now use builders. The downside is that it's a breaking change.

withXXXX() methods no longer create a new instance.  They instead
modify the instance, and return this.  This removes any possible
misunderstanding is using the withXXXX() calls individually (without
chaining them).

This change also ensures all the 'OpenGL 3.2 and newer' examples on the
wiki set up the display correctly (the examples assume mutability)

Also see http://lwjgl.org/forum/index.php/topic,4708.0.html
@gmacd
Copy link
Author

gmacd commented Nov 21, 2012

I forgot to add that making these two classes immutable seems to give no real benefit, so making them mutable to make the API less surprising should be a good thing :-)

@Spasi
Copy link
Member

Spasi commented Nov 21, 2012

The idea behind the current ContextAttribs API is:

a) It makes clear that once you create a context using it, there's no way to modify its settings. The only way to change anything is by destroying the context and recreating it with another ContextAttribs instance.

b) There's currently no way to get a ContextAttribs instance from an existing context. The only way to have that information available is to hang on to the instance you used when creating the context. Without immutability, there'd be no guarantees that information would be correct.

Anyway, this a low-risk change and I understand how the withXXXX pattern might be confusing if someone hasn't used it before. But if I were to do it again, I'd keep ContextAttribs immutable and provide a Builder/Factory for creating instances of it. What do you think?

@ghost ghost assigned Spasi Nov 21, 2012
@gmacd
Copy link
Author

gmacd commented Nov 21, 2012

Addressing your points, I would say that It's only clear if you've read the API code/docs fairly carefully that it's immutable - reading code that uses the API gives no indication it's immutable, or that changing the ContextAttribs after the context has been created will have no effect.

My own preference would be to avoid the factory, leave the object to be mutable, and add the ability to get a read-only version of ContextAttribs from the existing context - perhaps an interface already exists, I can't remember and I'm at work ATM :-)

The use of a fluent API here works quite well, but only if it's mutable (in Java at least).

@grum
Copy link
Collaborator

grum commented Nov 21, 2012

We could use a fluid builder pattern for it. There is just no benefit in having modifiable objects (and only downsides).

@gmacd
Copy link
Author

gmacd commented Nov 21, 2012

In a language where immutable objects are the norm, then I'd agree.

In Java, the norm is to have mutable objects. If the class is to be immutable, then any methods that create new instances should be named in a more obvious manner: e.g. newWithXXX() or createWithXXX() (though that wouldn't read as well as what there is now).

(Of course I'm not saying you should never use immutable objects in Java, just that it should be clear what's happening)

@void256
Copy link

void256 commented Nov 21, 2012

@HaggisChan according to josh blochs "Effective Java" book it's a good idea to make all your classes immutable by default since this makes them much simpler to use/maintain. I kinda don't agree that in Java "mutable classes" are the default :)

As Spasi said:

But if I were to do it again, I'd keep ContextAttribs immutable and provide a Builder/Factory for creating instances of it. What do you think?

+1

@grum
Copy link
Collaborator

grum commented Nov 21, 2012

(Of course I'm not saying you should never use immutable objects in Java, just that it should be clear what's happening)

If fluid modification/building is your goal you should write a fluid builder that spits out immutable objects, this saves you having to worry about synchronisation of a mutable object (because that would be mandatory in this case).

@gmacd
Copy link
Author

gmacd commented Nov 24, 2012

Ok, so people here are big on immutability ;-) I've committed versions of PixelFormat and ContextAttribs that are both fully immutable, and now use builders. The downside is that it's a breaking change.

Thoughts?

}

public Builder(int bpp, int alpha, int depth, int stencil, int samples, int num_aux_buffers, int accum_bpp, int accum_alpha, boolean stereo, boolean floating_point) {
this.bpp = bpp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont check for < 0 here.

@philipwhiuk
Copy link
Contributor

Should probably be consistent on how we handle those values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants