Skip to content

revert #13064 #13401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
- Added `sugar.collect` that does comprehension for seq/set/table collections.
- Added `sugar.capture` for capturing some local loop variables when creating a closure.
This is an enhanced version of `closureScope`.
- Added `typetraits.lenTuple` to get number of elements of a tuple/type tuple,
and `typetraits.get` to get the ith element of a type tuple.
- Added `typetraits.genericParams` to return a tuple of generic params from a generic instantiation
- Added `os.normalizePathEnd` for additional path sanitization.
- Added `times.fromUnixFloat,toUnixFloat`, subsecond resolution versions of `fromUnix`,`toUnixFloat`.
- Added `wrapnils` module for chains of field-access and indexing where the LHS can be nil.
Expand Down
42 changes: 0 additions & 42 deletions lib/pure/typetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

export system.`$` # for backward compatibility

include "system/inclrtl"

proc name*(t: typedesc): string {.magic: "TypeTrait".}
## Returns the name of the given type.
##
Expand Down Expand Up @@ -71,46 +69,6 @@ proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".}
## Returns base type for distinct types, works only for distinct types.
## compile time error otherwise

import std/macros

macro lenTuple*(t: tuple): int {.since: (1, 1).} =
## Return number of elements of `t`
newLit t.len

macro lenTuple*(t: typedesc[tuple]): int {.since: (1, 1).} =
## Return number of elements of `T`
newLit t.len

since (1, 1):
template get*(T: typedesc[tuple], i: static int): untyped =
## Return `i`th element of `T`
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
type(default(T)[i])

macro genericParams*(T: typedesc): untyped {.since: (1, 1).} =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be removed IMO. The other stuff I agree should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be removed. The informal reason is: shitty typedesc. There is even a PR that I am currently working on that would make this solution impossible #11959

Copy link
Contributor

@Clyybber Clyybber Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point out whats bad about it here? (sorry for disrupting your crusade :p)
I think preventing to return typedesc is a very bad idea. I can understand it for procs, but not for templates/macros.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing against returning typedesc from a macro. But putting typedesc in any form of container, such as array or tuple is bad. What do you think (int,float) is? Is it the type for a tuple of int and float, or is it a tuple containing the type int and the type float? If you print the result of this proc you will get somthing that will be something different when parsed. There are probably more problems under the hood that I can't name, because typedesc always has hidden problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found out that this function does return a tuple type, not a tuple value. And and since a tuple type does not have accessors, there accessors were added to typetraits.

## return tuple of generic params for generic `T`
runnableExamples:
type Foo[T1, T2]=object
doAssert genericParams(Foo[float, string]) is (float, string)
result = newNimNode(nnkTupleConstr)
var impl = getTypeImpl(T)
expectKind(impl, nnkBracketExpr)
impl = impl[1]
while true:
case impl.kind
of nnkSym:
impl = impl.getImpl
continue
of nnkTypeDef:
impl = impl[2]
continue
of nnkBracketExpr:
for i in 1..<impl.len:
result.add impl[i]
break
else:
error "wrong kind: " & $impl.kind

when isMainModule:
static:
doAssert $type(42) == "int"
Expand Down
19 changes: 0 additions & 19 deletions tests/metatype/ttypetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,6 @@ block distinctBase:
doAssert($distinctBase(typeof(b2)) == "string")
doAssert($distinctBase(typeof(c2)) == "int")

block genericParams:
type Foo[T1, T2]=object
doAssert genericParams(Foo[float, string]) is (float, string)
type Foo1 = Foo[float, int]
doAssert genericParams(Foo1) is (float, int)
type Foo2 = Foo[float, Foo1]
doAssert genericParams(Foo2) is (float, Foo[float, int])
doAssert genericParams(Foo2) is (float, Foo1)
doAssert genericParams(Foo2).get(1) is Foo1
doAssert (int,).get(0) is int
doAssert (int, float).get(1) is float
static: doAssert (int, float).lenTuple == 2
static: doAssert (1, ).lenTuple == 1
static: doAssert ().lenTuple == 0


##############################################
# bug 13095

Expand Down Expand Up @@ -141,9 +125,6 @@ block genericHead:
type Hoo = Goo[Moo[float]]
type Koo = genericHead(Hoo)
doAssert Koo is Goo
doAssert genericParams(Hoo) is (Moo[float],)
doAssert genericParams(Hoo).get(0) is Moo[float]
doAssert genericHead(genericParams(Hoo).get(0)) is Moo

type Foo2Inst = Foo2[int, float]
doAssert FooInst.default == Foo2Inst.default
Expand Down