Jump to page: 1 2
Thread overview
Functions that return type
Jan 16, 2016
data pulverizer
Jan 16, 2016
data pulverizer
Jan 16, 2016
data pulverizer
Jan 17, 2016
Ali Çehreli
Jan 17, 2016
Timon Gehr
Jan 17, 2016
data pulverizer
Jan 19, 2016
Timon Gehr
Jan 20, 2016
blm768
Jan 20, 2016
burjui
Jan 20, 2016
BLM768
Jan 21, 2016
thedeemon
Jan 16, 2016
rsw0x
Jan 16, 2016
anonymous
Jan 16, 2016
sarn
January 16, 2016
Is it possible to create a function that returns Type like typeof() does? Something such as:

Type returnInt(){
    return int;
}

More to the point what is the Type of a type such as int?

Thanks
January 16, 2016
On Saturday, 16 January 2016 at 21:22:15 UTC, data pulverizer wrote:
> Is it possible to create a function that returns Type like typeof() does? Something such as:
>
> Type returnInt(){
>     return int;
> }
>
> More to the point what is the Type of a type such as int?
>
> Thanks

p.s. I am aware I could do typeof(1) to return int, but I am looking for something more elegant and some understanding.
January 16, 2016
On Saturday, 16 January 2016 at 21:22:15 UTC, data pulverizer wrote:
> Is it possible to create a function that returns Type like typeof() does? Something such as:
>
> Type returnInt(){
>     return int;
> }

Functions return values, not types. You would use a template to "return" a type.

>
> More to the point what is the Type of a type such as int?
>
> Thanks

What is the value of a value such as 9? A type is a type, it does not have a type.

If this is not clear, I can try to make it clearer.
January 16, 2016
On Saturday, 16 January 2016 at 21:22:15 UTC, data pulverizer wrote:
> Is it possible to create a function that returns Type like typeof() does? Something such as:
>
> Type returnInt(){
>     return int;
> }

No. A function cannot return a type. A template can evaluate to a type, though:

----
template returnInt(){
    alias returnInt = int;
}
----

> More to the point what is the Type of a type such as int?

Types don't have types. You can check if something is a type with an IsExpression: `is(T)` is true if T is a type.
January 16, 2016
On Saturday, 16 January 2016 at 21:22:15 UTC, data pulverizer wrote:
> Is it possible to create a function that returns Type like typeof() does? Something such as:
>
> Type returnInt(){
>     return int;
> }

A type itself isn't a runtime value.  I think the closest thing is a TypeInfo object:

https://dlang.org/library/object/type_info.html
https://dlang.org/spec/expression.html#TypeidExpression
January 16, 2016
On Saturday, 16 January 2016 at 21:59:22 UTC, data pulverizer wrote:
> On Saturday, 16 January 2016 at 21:22:15 UTC, data pulverizer wrote:
>> Is it possible to create a function that returns Type like typeof() does? Something such as:
>>
>> Type returnInt(){
>>     return int;
>> }
>>
>> More to the point what is the Type of a type such as int?
>>
>> Thanks
>
> p.s. I am aware I could do typeof(1) to return int, but I am looking for something more elegant and some understanding.

Thanks for all the answers. I guess I have been writing a lot of julia where I take creating arrays and tuples of types for granted. In this case types are of type DataType. I am aware that you can create tuples of types in D, but then it cannot be easily manipulated e.g. (int, float)[0] = string or similar. You have to immediately alias it and there are a limited number of operations you can do with the resulting type. I guess the constraints are that of a static language.
January 16, 2016
On 01/16/2016 02:50 PM, data pulverizer wrote:

> I guess I have been writing a lot of julia where I take
> creating arrays and tuples of types for granted. In this case
> types are of type DataType. [...] I guess the constraints are
> that of a static language.

Exactly. I am sure every D compiler has the equivalent of DataType and every type used in a program has instances of it but such information disappears by the end of compilation, except their properties in the form of TypeInfo as sarn has already mentioned.

Ali

January 17, 2016
On 01/16/2016 11:50 PM, data pulverizer wrote:
> I guess the constraints are that of a static language.

(This is not true.)
January 17, 2016
On Sunday, 17 January 2016 at 02:08:06 UTC, Timon Gehr wrote:
> On 01/16/2016 11:50 PM, data pulverizer wrote:
>> I guess the constraints are that of a static language.
>
> (This is not true.)

Could you please explain?
January 19, 2016
On 01/17/2016 08:09 PM, data pulverizer wrote:
> On Sunday, 17 January 2016 at 02:08:06 UTC, Timon Gehr wrote:
>> On 01/16/2016 11:50 PM, data pulverizer wrote:
>>> I guess the constraints are that of a static language.
>>
>> (This is not true.)
>
> Could you please explain?

E.g., a few of the systems discussed at

https://ncatlab.org/nlab/show/pure+type+system

would be a fine basis for a "static language" that supports returning types from functions.
« First   ‹ Prev
1 2