[fpc-pascal] Syntax changes suggestions

Ryan Joseph ryan at thealchemistguild.com
Tue Jul 17 18:10:01 CEST 2018



> On Jul 16, 2018, at 11:20 PM, Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> Because they are programmers. They simply do it their way. And I'm especially talking about code written by two different programmers, one storing the instance somewhere and the other passing in an "auto" instance without knowing that the other programmer is storing it somewhere.
>> Auto is a good idea. Allocating the “auto” classes the memory backend on the stack (my “stack alias” idea) is even better because you don’t need the memory manager too be involved.
> No, it's not. And we don't *want* to change the paradigm of TObject.

The reason we propose these things is because of a manifest observable need that we experience daily. We’re not proposing things like making Pascal garbage collected or ARC for all objects like new languages are. These are practical and optional solutions that programmers can use depending on their needs.

Firs off, yes I know this isn’t Pascal objects but look at this entirely predictable block of code that I found after searching for 10 seconds. Dynamically allocating memory which I *know* beyond a shadow of a doubt will only survive this function. There’s not going to be any passing around. I know this. 

Predictably at the end of the function I review all the memory I allocated and free it. Why couldn’t I just tell the compiler I want this freed at the time I declare the variable? I know then and I don’t want to think about it again. This is such an obvious pattern to optimize in the language. This is not that crazy guys.

	colorSpace := CGColorSpaceCreateDeviceRGB;
	bitmapInfo := kCGImageAlphaFirst or kCGBitmapByteOrder32Little;
	provider := CGDataProviderCreateWithData(nil, bytes, bytesCount, nil);
	imageRef := CGImageCreate(width, height, 8, 32, bytesPerRow, colorSpace, bitmapInfo, provider, nil, 1, kCGRenderingIntentDefault);

	finalImage := NSImage.alloc.initWithCGImage_size(imageRef, NSMakeSize(width, height));
	imageData := finalImage.TIFFRepresentation;
	imageRep := NSBitmapImageRep.imageRepWithData(imageData);
	//imageProps := NSDictionary.dictionaryWithObject_forKey(NSNumber.numberWithFloat(1), NSImageCompressionFactor);
	imageData := imageRep.representationUsingType_properties(fileType, imageProps);
	imageData.writeToFile_atomically(NSSTR(path), false);
	
	// <——————————————— freeing stuff ———————————————>
	CFRelease(provider);
	CFRelease(imageRef);
	CFRelease(colorSpace);
	finalImage.release;

var
  colorSpace: CGColorSpaceRef;
  provider: CGDataProviderRef;
  imageRef: CGImageRef;
  finalImage: NSImage;


Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list