digitalmars.D.learn - Functions that return type
- data pulverizer (7/7) Jan 16 2016 Is it possible to create a function that returns Type like
- data pulverizer (4/11) Jan 16 2016 p.s. I am aware I could do typeof(1) to return int, but I am
- data pulverizer (10/24) Jan 16 2016 Thanks for all the answers. I guess I have been writing a lot of
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/10) Jan 16 2016 Exactly. I am sure every D compiler has the equivalent of DataType and
- Timon Gehr (2/3) Jan 16 2016 (This is not true.)
- data pulverizer (2/5) Jan 17 2016 Could you please explain?
- Timon Gehr (5/11) Jan 19 2016 E.g., a few of the systems discussed at
- blm768 (7/10) Jan 19 2016 I'm playing with the design of such a language myself. Basically,
- rsw0x (7/14) Jan 16 2016 Functions return values, not types. You would use a template to
- anonymous (11/17) Jan 16 2016 No. A function cannot return a type. A template can evaluate to a
- sarn (6/11) Jan 16 2016 A type itself isn't a runtime value. I think the closest thing
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
Jan 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? Thanksp.s. I am aware I could do typeof(1) to return int, but I am looking for something more elegant and some understanding.
Jan 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: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.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? Thanksp.s. I am aware I could do typeof(1) to return int, but I am looking for something more elegant and some understanding.
Jan 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
Jan 16 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.)
Jan 16 2016
On Sunday, 17 January 2016 at 02:08:06 UTC, Timon Gehr wrote:On 01/16/2016 11:50 PM, data pulverizer wrote:Could you please explain?I guess the constraints are that of a static language.(This is not true.)
Jan 17 2016
On 01/17/2016 08:09 PM, data pulverizer wrote:On Sunday, 17 January 2016 at 02:08:06 UTC, Timon Gehr wrote: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.On 01/16/2016 11:50 PM, data pulverizer wrote:Could you please explain?I guess the constraints are that of a static language.(This is not true.)
Jan 19 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'm playing with the design of such a language myself. Basically, anything can create/use/return type objects, but a variable of a particular type can only be instantiated using an immutable type object whose value is known at compile time. It's not very far along, though. Right now, I have a "compiler" that parses integers and parentheses. ;)I guess the constraints are that of a static language.(This is not true.)
Jan 19 2016
On Wednesday, 20 January 2016 at 04:27:27 UTC, blm768 wrote:It's not very far along, though. Right now, I have a "compiler" that parses integers and parentheses. ;)That's alright. Parsing and AST construction are trivial with S-expressions (Lisp-like syntax), so if you use them for the early stages of development, you can focus on the type system. When you're done with types, you can switch to making a better grammar for your language.
Jan 20 2016
On Wednesday, 20 January 2016 at 10:04:03 UTC, burjui wrote:That's alright. Parsing and AST construction are trivial with S-expressions (Lisp-like syntax), so if you use them for the early stages of development, you can focus on the type system. When you're done with types, you can switch to making a better grammar for your language.True. I'd been playing with the idea of having multiple syntactic "front-ends" anyway (mainly for the purpose of making DSLs), so it wouldn't be too much of a stretch to use an S-expression syntax. One problem, though, is that I'd either have to extend that syntax to support some of the constructs I want (i.e array literals) or create a bunch of variadic constructor functions (which could be evaluated at compile time). Of course, S-expression syntax is kind of designed to be extensible... That's all off-topic, though. ;)
Jan 20 2016
On Wednesday, 20 January 2016 at 04:27:27 UTC, blm768 wrote:This is usually possible in dependently typed languages such as Idris, Agda or Coq. Check out Idris, it's rather small but very nice and interesting.I'm playing with the design of such a language myself. Basically, anything can create/use/return type objectsI guess the constraints are that of a static language.(This is not true.)
Jan 20 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? ThanksWhat 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.
Jan 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.
Jan 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
Jan 16 2016