June 14, 2012
On 14.06.2012 20:32, Roman D. Boiko wrote:
> On Thursday, 14 June 2012 at 16:24:43 UTC, Jonathan M Davis wrote:
>> On Thursday, June 14, 2012 17:32:03 Roman D. Boiko wrote:
>>> I don't know how to put a variable of type float to the heap
>>
>> auto f = new float;
>> *f = 2.1;
>>
>> - Jonathan M Davis
>
> Except for immutable I would need to cast when passing into a function.
> That's dangerous, given that *f might be changed later. But looks like
> Timon's suggestion from my other question should work.
>
> immutable a = [2.1].ptr;
>

That or pack the logic into a pure function, e.g. this should work:

immutable ap = newPureFloat(3.1415);

float* newPureFloat(float val)pure
{
	float* p = new float;
	*p = val;
	return p;
}

-- 
Dmitry Olshansky
June 14, 2012
On Thursday, June 14, 2012 21:27:58 Dmitry Olshansky wrote:
> On 14.06.2012 20:32, Roman D. Boiko wrote:
> > On Thursday, 14 June 2012 at 16:24:43 UTC, Jonathan M Davis wrote:
> >> On Thursday, June 14, 2012 17:32:03 Roman D. Boiko wrote:
> >>> I don't know how to put a variable of type float to the heap
> >> 
> >> auto f = new float;
> >> *f = 2.1;
> >> 
> >> - Jonathan M Davis
> > 
> > Except for immutable I would need to cast when passing into a function. That's dangerous, given that *f might be changed later. But looks like Timon's suggestion from my other question should work.
> > 
> > immutable a = [2.1].ptr;
> 
> That or pack the logic into a pure function, e.g. this should work:
> 
> immutable ap = newPureFloat(3.1415);
> 
> float* newPureFloat(float val)pure
> {
> float* p = new float;
> *p = val;
> return p;
> }

Yeah, I was just thinking that maybe we should make a generic function for this and add it to Phobos. Something like

auto makePtr(T, U)(U value) if(is(U : T)) {}
auto makePtr(T, Args...)(Args args) {}

where the first one works with primitive types and the second one works with structs. Then you could do

auto f = makePtr!(immutable float)(2.1);

I think that I'll have to see about throwing together something like that tonight and create a pull request.

- Jonathan M Davis
June 14, 2012
On 14.06.2012 21:43, Jonathan M Davis wrote:
> On Thursday, June 14, 2012 21:27:58 Dmitry Olshansky wrote:
>> On 14.06.2012 20:32, Roman D. Boiko wrote:
>>> On Thursday, 14 June 2012 at 16:24:43 UTC, Jonathan M Davis wrote:
>>>> On Thursday, June 14, 2012 17:32:03 Roman D. Boiko wrote:
>>>>> I don't know how to put a variable of type float to the heap
>>>>
>>>> auto f = new float;
>>>> *f = 2.1;
>>>>
>>>> - Jonathan M Davis
>>>
>>> Except for immutable I would need to cast when passing into a function.
>>> That's dangerous, given that *f might be changed later. But looks like
>>> Timon's suggestion from my other question should work.
>>>
>>> immutable a = [2.1].ptr;
>>
>> That or pack the logic into a pure function, e.g. this should work:
>>
>> immutable ap = newPureFloat(3.1415);
>>
>> float* newPureFloat(float val)pure
>> {
>> float* p = new float;
>> *p = val;
>> return p;
>> }
>
> Yeah, I was just thinking that maybe we should make a generic function for
> this and add it to Phobos. Something like
>
> auto makePtr(T, U)(U value) if(is(U : T)) {}
> auto makePtr(T, Args...)(Args args) {}

I think make new would be more self-explanatory. Ptr doesn't imply heap.
Other then this, it looks useful.
(I'd love to see an optional allocator parameter... but have to wait I guess)
>
> where the first one works with primitive types and the second one works with
> structs. Then you could do
>
> auto f = makePtr!(immutable float)(2.1);
>
> I think that I'll have to see about throwing together something like that
> tonight and create a pull request.
>
> - Jonathan M Davis


-- 
Dmitry Olshansky
June 14, 2012
On Thursday, 14 June 2012 at 17:44:11 UTC, Jonathan M Davis wrote:
> I was just thinking that maybe we should make a generic function for
> this and add it to Phobos. Something like
>
> auto makePtr(T, U)(U value) if(is(U : T)) {}
> auto makePtr(T, Args...)(Args args) {}
>
> where the first one works with primitive types and the second one works with
> structs. Then you could do
>
> auto f = makePtr!(immutable float)(2.1);
>
> I think that I'll have to see about throwing together something like that
> tonight and create a pull request.
>
> - Jonathan M Davis
Am I right that [2.0].ptr (instead of 2.0 could be a struct literal) doesn't involve copying, while such function would?
June 14, 2012
On 14.06.2012 21:57, Roman D. Boiko wrote:
> On Thursday, 14 June 2012 at 17:44:11 UTC, Jonathan M Davis wrote:
>> I was just thinking that maybe we should make a generic function for
>> this and add it to Phobos. Something like
>>
>> auto makePtr(T, U)(U value) if(is(U : T)) {}
>> auto makePtr(T, Args...)(Args args) {}
>>
>> where the first one works with primitive types and the second one
>> works with
>> structs. Then you could do
>>
>> auto f = makePtr!(immutable float)(2.1);
>>
>> I think that I'll have to see about throwing together something like that
>> tonight and create a pull request.
>>
>> - Jonathan M Davis
> Am I right that [2.0].ptr (instead of 2.0 could be a struct literal)
> doesn't involve copying, while such function would?

Copying what?  One word is surely cheap ;)

-- 
Dmitry Olshansky
June 14, 2012
On Thursday, 14 June 2012 at 17:58:25 UTC, Dmitry Olshansky wrote:
> On 14.06.2012 21:57, Roman D. Boiko wrote:
>> Am I right that [2.0].ptr (instead of 2.0 could be a struct literal)
>> doesn't involve copying, while such function would?
>
> Copying what?  One word is surely cheap ;)
That was just curiosity, not objection. Also, struct literal could correspond to a larger instance.

June 14, 2012
On Thursday, June 14, 2012 21:50:31 Dmitry Olshansky wrote:
> On 14.06.2012 21:43, Jonathan M Davis wrote:
> > On Thursday, June 14, 2012 21:27:58 Dmitry Olshansky wrote:
> >> On 14.06.2012 20:32, Roman D. Boiko wrote:
> >>> On Thursday, 14 June 2012 at 16:24:43 UTC, Jonathan M Davis wrote:
> >>>> On Thursday, June 14, 2012 17:32:03 Roman D. Boiko wrote:
> >>>>> I don't know how to put a variable of type float to the heap
> >>>> 
> >>>> auto f = new float;
> >>>> *f = 2.1;
> >>>> 
> >>>> - Jonathan M Davis
> >>> 
> >>> Except for immutable I would need to cast when passing into a function. That's dangerous, given that *f might be changed later. But looks like Timon's suggestion from my other question should work.
> >>> 
> >>> immutable a = [2.1].ptr;
> >> 
> >> That or pack the logic into a pure function, e.g. this should work:
> >> 
> >> immutable ap = newPureFloat(3.1415);
> >> 
> >> float* newPureFloat(float val)pure
> >> {
> >> float* p = new float;
> >> *p = val;
> >> return p;
> >> }
> > 
> > Yeah, I was just thinking that maybe we should make a generic function for this and add it to Phobos. Something like
> > 
> > auto makePtr(T, U)(U value) if(is(U : T)) {}
> > auto makePtr(T, Args...)(Args args) {}
> 
> I think make new would be more self-explanatory. Ptr doesn't imply heap.
> Other then this, it looks useful.
> (I'd love to see an optional allocator parameter... but have to wait I
> guess)

Well, we use make with std.container already for essentially the same thing, only it's specifically for containers.

- Jonathan M Davis
June 14, 2012
On Thursday, 14 June 2012 at 18:01:06 UTC, Jonathan M Davis wrote:
>> I think make new would be more self-explanatory. Ptr doesn't imply heap.
>> Other then this, it looks useful.
>> (I'd love to see an optional allocator parameter... but have to wait I
>> guess)
>
> Well, we use make with std.container already for essentially the same thing,
> only it's specifically for containers.
>
> - Jonathan M Davis
Dmitry probably suggested makeNew(...)

June 14, 2012
On 14.06.2012 22:03, Roman D. Boiko wrote:
> On Thursday, 14 June 2012 at 18:01:06 UTC, Jonathan M Davis wrote:
>>> I think make new would be more self-explanatory. Ptr doesn't imply heap.
>>> Other then this, it looks useful.
>>> (I'd love to see an optional allocator parameter... but have to wait I
>>> guess)
>>
>> Well, we use make with std.container already for essentially the same
>> thing,
>> only it's specifically for containers.
>>
>> - Jonathan M Davis
> Dmitry probably suggested makeNew(...)
>
Yup, what he said ^ :)

-- 
Dmitry Olshansky
June 14, 2012
On 06/14/2012 06:42 PM, Jonathan M Davis wrote:
> I wish that you could do
>
> auto f = new float(2.1);
>
> and therefore
>
> auto f = new immutable(float)(2.1);
>
> but you can't.

This seems to be an arbitrary limitation.
Imho it should be fixed. Probably it was missed because allocating a
primitive type on the heap is not a very common operation.