Jump to page: 1 2
Thread overview
How to get element type of a slice?
Aug 17, 2021
Ferhat Kurtulmuş
Aug 17, 2021
jfondren
Aug 17, 2021
Ferhat Kurtulmuş
Aug 17, 2021
Ferhat Kurtulmuş
Aug 19, 2021
Jesse Phillips
Aug 19, 2021
Jesse Phillips
Aug 19, 2021
jfondren
Aug 21, 2021
Jesse Phillips
Aug 23, 2021
Ferhat Kurtulmuş
Aug 17, 2021
Paul Backus
Aug 17, 2021
Ferhat Kurtulmuş
Aug 17, 2021
drug
Aug 17, 2021
Ferhat Kurtulmuş
Aug 17, 2021
Ferhat Kurtulmuş
Aug 18, 2021
jmh530
August 17, 2021

Hello folks,

Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance.

    static if (isArray!VecPoint){
        VecPoint dummy;
        alias Point = typeof(dummy[0]);
    } else static if (isRandomAccessRange!VecPoint){
        alias ASeq2 = TemplateArgsOf!VecPoint;
        alias Point = ASeq2[0];
    } else
        static assert(0, typeof(VecPoint).stringof ~ " type is not supported");

Ferhat

August 17, 2021

On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote:

>

Hello folks,

Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance.

    static if (isArray!VecPoint){
        VecPoint dummy;
        alias Point = typeof(dummy[0]);
    } else static if (isRandomAccessRange!VecPoint){
        alias ASeq2 = TemplateArgsOf!VecPoint;
        alias Point = ASeq2[0];
    } else
        static assert(0, typeof(VecPoint).stringof ~ " type is not supported");

Ferhat

This one's not in std.traits:

import std.range : ElementType;

struct Point { int x, y; }

unittest {
    Point[] points;
    assert(is(ElementType!(typeof(points)) == Point));
}
August 17, 2021

On Tuesday, 17 August 2021 at 12:26:36 UTC, jfondren wrote:

>

On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote:

>

[...]

This one's not in std.traits:

import std.range : ElementType;

struct Point { int x, y; }

unittest {
    Point[] points;
    assert(is(ElementType!(typeof(points)) == Point));
}

Awesome! Have a great day.

August 17, 2021

On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote:

>

Hello folks,

Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance.

typeof(T.init[0])

Note that std.range.ElementType will probably not give the result you expect for character arrays (such as char[] and wchar[]), due to autodecoding.

August 17, 2021

On Tuesday, 17 August 2021 at 12:26:36 UTC, jfondren wrote:

>

On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote:

>

[...]

This one's not in std.traits:

import std.range : ElementType;

struct Point { int x, y; }

unittest {
    Point[] points;
    assert(is(ElementType!(typeof(points)) == Point));
}

Hey, thank you again but, I don't want an instance of Point[] I need:

alias T = Point[];

alias ElementOfPointSlice = .... // element type of T

August 17, 2021

On Tuesday, 17 August 2021 at 12:32:45 UTC, Paul Backus wrote:

>

On Tuesday, 17 August 2021 at 12:21:31 UTC, Ferhat Kurtulmuş wrote:

>

Hello folks,

Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance.

typeof(T.init[0])

Note that std.range.ElementType will probably not give the result you expect for character arrays (such as char[] and wchar[]), due to autodecoding.

Thank you. This one looks better.

August 17, 2021
17.08.2021 15:21, Ferhat Kurtulmuş пишет:
> Hello folks,
> 
> Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance.
> 
> ```d
>      static if (isArray!VecPoint){
>          VecPoint dummy;
>          alias Point = typeof(dummy[0]);
>      } else static if (isRandomAccessRange!VecPoint){
>          alias ASeq2 = TemplateArgsOf!VecPoint;
>          alias Point = ASeq2[0];
>      } else
>          static assert(0, typeof(VecPoint).stringof ~ " type is not supported");
> ```
> 
> Ferhat

https://dlang.org/library/std/range/primitives/element_type.html
August 17, 2021

On 8/17/21 8:21 AM, Ferhat Kurtulmuş wrote:

>

Hello folks,

Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance.

     static if (isArray!VecPoint){
         VecPoint dummy;
         alias Point = typeof(dummy[0]);
     } else static if (isRandomAccessRange!VecPoint){
         alias ASeq2 = TemplateArgsOf!VecPoint;
         alias Point = ASeq2[0];
     } else
         static assert(0, typeof(VecPoint).stringof ~ " type is not supported");

If you want the element type of a range (i.e. the thing returned by range.front), you can use ElementType!T (from std.range.primitives).

This returns the element type of the range, which for every array except character arrays, gives you the element type of the array.

If you want always the element type of the array, even for auto-decoded ranges, use ElementEncodingType!T.

If you know it's an array, you can just use Paul's solution.

Your isRandomAccessRange branch seems very suspect.

-Steve

August 17, 2021
On Tuesday, 17 August 2021 at 12:49:02 UTC, drug wrote:
> 17.08.2021 15:21, Ferhat Kurtulmuş пишет:
>> [...]
>
> https://dlang.org/library/std/range/primitives/element_type.html

Yes, that is neat. Thank you.
August 17, 2021

On Tuesday, 17 August 2021 at 13:14:44 UTC, Steven Schveighoffer wrote:

>

On 8/17/21 8:21 AM, Ferhat Kurtulmuş wrote:

>

Hello folks,

Hope everyone is doing fine. Considering the following code, in the first condition, I am extracting the type Point from the slice Point[]. I searched in the std.traits, and could not find a neater solution something like ElementTypeOf!T. Is there any neater solution for it? Thanks in advance.

     static if (isArray!VecPoint){
         VecPoint dummy;
         alias Point = typeof(dummy[0]);
     } else static if (isRandomAccessRange!VecPoint){
         alias ASeq2 = TemplateArgsOf!VecPoint;
         alias Point = ASeq2[0];
     } else
         static assert(0, typeof(VecPoint).stringof ~ " type is not supported");

If you want the element type of a range (i.e. the thing returned by range.front), you can use ElementType!T (from std.range.primitives).

This returns the element type of the range, which for every array except character arrays, gives you the element type of the array.

If you want always the element type of the array, even for auto-decoded ranges, use ElementEncodingType!T.

If you know it's an array, you can just use Paul's solution.

Your isRandomAccessRange branch seems very suspect.

-Steve

Very informative, thanks. My code is lying here1. I want my struct to accept 2d static arrays, random access ranges, and "std.container.Array". I could achieve it with its present form, and I will probably slightly modify it based on your comments.

« First   ‹ Prev
1 2